Error here TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object

Viewed 6044

Hi I am trying to upload a base 64 file from NodeJs to firebase storage(google cloud storage) using the below given code. But I am getting an error stating

Error here TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object at validateString (internal/validators.js:125:11)

the code is:

const key = require('../key.json');
const {Storage} = require('@google-cloud/storage');
//var gcs = require('@google-cloud/storage');
exports.uploadVideo = functions.https.onRequest((req, res) => {

    const gcs =new Storage({
        projectId: "<my-project-id>",
        keyFilename: key
      });

      const bucket = gcs.bucket("example.appspot.com");
      const gcsname = 'test.pdf';
      const file = bucket.file(gcsname);
      var pdfdata = "xNC9YUmVmU3RtIDE1NzQ+Pg0Kc3RhcnR4cmVmDQoyMTY5DQolJUVPRg==";
      var buff = Buffer.from(pdfdata, 'binary').toString('utf-8');
      const stream = file.createWriteStream({
        metadata: {
          contentType: 'application/pdf'
        }
      });

      stream.on('error', (err) => {
        console.log("Error here",err);
      });
      stream.on('finish', () => {
        console.log(gcsname);
      });
      stream.end(new Buffer.from(buff, 'base64'));})

Can someone suggest me a way out? I can't understand the problem. Thanks

3 Answers

It means that the program is expecting the path argument to of be type string, example: "path/to/whatever". And you're giving it an object - example: { path: "path/to/whatever" }

As the error said "TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined"

It trying to grab from path, which you should add those variables into your .env file to the root when looking for path ..it will grab the right variables. Let me know if you still have difficulties..

You can refer to Express Routing with Google Cloud Functions for more details informations.

This will be fixed if you are using "react-scripts" "^3.x.x" replace it to "^3.4.1" version or latest one. Please make sure you deleted the node_modules folder before npm/yarn install

Related