google-api-nodejs-client: TypeError: Cannot read property 'data' of undefined on the sample code....

Trying to run the sample code for the drive list stuff and am getting this error:

TypeError: Cannot read property 'data' of undefined at runSample (c:\Users\Konstantin\Desktop\USN\JS\newCode.js:15:19) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7)

Environment details

  • OS: Windows 10
  • Node.js version: 8.11.4
  • npm version: 6.4.1
  • googleapis version: 33.0.0

Steps to reproduce

  1. Run the Drive list sample code…

Bonus question: How can I create a token to use so that I don’t have to login each time the script is run?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Sure thing! 😃 I just have it inside VS Code and press the run button tbh… But here is the script:

'use strict';

const {google} = require('googleapis');
const sampleClient = require('./sampleclient');

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

async function runSample (query) {
  const params = { 
      pageSize: 3,
      fields: "files(id, name, parents, size)"
    };
  params.q = query;
  const res = await drive.files.list(params);
  console.log(res.data);
  return res.data;
}

if (module === require.main) {
  const scopes = ['https://www.googleapis.com/auth/drive'];
  sampleClient.authenticate(scopes)
    .then(c => runSample())
    .catch(console.error);
}

module.exports = {
  runSample,
  client: sampleClient.oAuth2Client
};

It gets executed with node newCode.js

Ahhhh. That’s a pretty old version of googleapis 😃 Can you please upgrade to 33, and remove the dependency on google-auth-library?

Yeah, just did 😄