Mhmd Backer Shehadi
Mhmd Backer Shehadi

Reputation: 589

Node js Google drive api error

I am trying to use Google Node.js client library to access the Google Drive API so i can upload some files to my drive account but I am facing this error :

drive.files.create({
^

ReferenceError: drive is not defined
    at Object.<anonymous> (/Applications/MAMP/htdocs/google_drive/upload.js:16:1)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

I am using the docs example:

var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');

var fileMetadata = {
    'name': 'photo.jpg'
};
var media = {
    mimeType: 'image/jpeg',
    body: fs.createReadStream('files/photo.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);
    }
});

Can anybody help to resolve this issue?.

Upvotes: 0

Views: 1238

Answers (2)

Feranmi
Feranmi

Reputation: 101

I believe your declaration of const google should use a destructuring assignment instead. As in,

const {google} = require(googleapis);

You then declare drive like so:

const drive = google.drive({version: 'v3',  auth});

Where auth is the authorized OAuth2 client.

Upvotes: 2

NullDev
NullDev

Reputation: 7303

You need to define it first.

var drive = google.drive("v3");

Look up the appropriate approach on Google's documentation:

https://github.com/google/google-api-nodejs-client/#authorizing-and-authenticating

Upvotes: 2

Related Questions