Search code examples
node.jsapigoogle-drive-apidrive

Using Google drive API to upload files in G-Drive facing issue


*Hi, Using Google Drive API to upload files in the Drive. The file gets uploaded successfully but in response, the file ID is showing undefined. I have to use the file id for further code *

function uplodeFile(auth){
    const drive = google.drive({version: 'v3', auth});
    var fileMetadata = {
        'name': 'error.jpg'
      };
      var media = {
        mimeType: 'image/jpeg',
        body: fs.createReadStream('Capture.JPG')
      };
      drive.files.create({
        resource: fileMetadata,
        media: media,
        fields: 'id'
      }, function (err, file) {
        if (err) {
          // Handle error
          console.error(err);
        } else {
          console.log('File Id: ', file.id);
        }
      });
}

This is the Snippet of the response


Solution

  • If you are using the latest version of googleapis for Node.js, how about the following modification?

    From:

    console.log('File Id: ', file.id);
    

    To:

    console.log('File Id: ', file.data.id);
    
    • In the current stage, the latest version is v61.0.0.

    Reference: