google-api-nodejs-client: [Help] Google Drive export example not working

I have the code from the drive export example with the sampleClient and it’s producing an error.

(node:3593) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘on’ of undefined

function download(fileId) {
    drive.files.get({
        fileId: fileId
    }, (err, metadata) => {
        if (err) {
            throw err;
        }
        console.log('Downloading %s...', metadata.name);
        const dest = fs.createWriteStream(metadata.name + '.csv');

        drive.files.export({
            fileId: fileId,
            mimeType: 'text/csv'
        })
            .on('error', err => {
                console.error('Error downloading file!');
                throw err;
            })
            .pipe(dest);

        dest
            .on('finish', () => {
                console.log('Downloaded %s!', metadata.name);
                process.exit();
            })
            .on('error', err => {
                console.error('Error writing file!');
                throw err;
            });
    });
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 20 (3 by maintainers)

Most upvoted comments

finally i made a export script working for me, i hope help someone else;

function download(fileId, name, done) {
        const dest = fs.createWriteStream(name + '.csv');
        drive.files.export({
            fileId: fileId,
            mimeType: 'text/csv'
        }, {
            responseType: 'stream'
        },function(err, response){
            if(err)return done(err);
            
            response.data.on('error', err => {
                done(err);
            }).on('end', ()=>{
                done();
            })
            .pipe(dest);
       });
}

is important the responseType: "stream" to have a stream in response.data. options are passed to axios.request, here you can find all options

I’m having the same issue running the sample code from the drive download example. (https://github.com/google/google-api-nodejs-client/blob/master/samples/drive/download.js)

TypeError: Cannot read property ‘on’ of undefined

If you prefer async/await… this worked for me:

const drive = google.drive({ version: 'v3', auth });
const dest = fs.createWriteStream('/tmp/resume.pdf');

const { data } = await drive.files.export(
  {
    fileId: 'xxxxx',
    mimeType: 'application/pdf',
  },
  {
    responseType: 'stream',
  }
);

data
  .on('end', function () {
    console.log('Done');
  })
  .on('error', function (err) {
    console.log('Error during download', err);
  })
  .pipe(dest);

The docs here are still out of date: https://developers.google.com/drive/api/v3/manage-downloads#node.js

Just wanted to let someone know the official documentation still isn’t updated. It’s very frustrating when the examples don’t work and pretty much nulls the value of any documentation when you have to debug a library just to download a file. 😦

FYI, example to export(…) without using a stream , to pdf for example:

 drive.files.export({
            fileId: fileId,
            mimeType: 'application/pdf'
        }, {
            responseType: 'arraybuffer'
        },function(err, response){
            //response.data can be written to file as expected
       });

Here is one if the docs pages that need the fix in their examples https://developers.google.com/drive/api/v3/manage-downloads

This must have broke when axios was added.

I see it now in the axios documentation:

// `responseType` indicates the type of data that the server will respond with
// options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
responseType: 'json', // default