Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Google Drive api Download file error

This is my code:

const fs = require('fs');
const { google } = require('googleapis');
const Auth = require('../authorization/auth_drive/auth');

// get data
var gotData = function downloadFiles(fileId, callback) {
    let content = fs.readFileSync('./GEARS/authorization/client_secret.json');
    Auth(JSON.parse(content), (auth) => {
        //const returnData = [];
        const drive = google.drive({ version: 'v3', auth });

        const fileId = '';
        const dest = fs.createWriteStream('./test_files/test.png');

        drive.files.get({fileId: fileId, alt: 'media'})
        .on('end', () => {
            console.log('Done');
        })
        .on('error', err => {
            console.log('Error', err);
        })
        .pipe(dest);
    });
};

module.exports = gotData;

Following the google documentation LINK

All other functions I have tried, like List, Copy, Upload or Sheets Read&Write work perfectly for me, except for the Download. My application instantly crashes with the error

TypeError: Cannot read property 'on' of undefined

I have looked at Google's Sample of this very functionality, and I have not found anything that differs, so why does it work for Google Developers, while for me it doesn't?

I have not found a single person with this problem on google, so maybe, if you understand what should be done, you can see what I am doing wrong.

-- I will include THIS LINK, that is a Copy function I have, which works perfectly, in case it would be relevant.

Thanks for any advice.

like image 823
Richard Franek Avatar asked May 16 '18 14:05

Richard Franek


1 Answers

From your script, I thought that you may be using the googleapis of the version more than v24.0.0. So how about modifying the version of googleapis? Also I have experienced the same issue with your situation. In my case, the issue was removed by modifying the version of googleapis to v24.0.0.

When I used v25.0.0 - v30.0.0 of googleapis, the error occurs. When it used the googleapis of v24.0.0, the error was removed.

Note :

  • For the googleapis after the version of v25.0.0, some bugs for APIs and the options are reported. I believe that these bugs are removed in the future. So if for APIs and the options you use, some errors occur, please modify the version of googleapis, and try again. The cases which were solved by modifying the version are as follows.

References :

  • How do I update my google sheet in v4?
  • Create a gmail filter with Gmail API nodejs, Error: Filter doesn't have any criteria
  • Insufficient Permission when trying to create a folder on Google Drive via API(v3)
  • Youtube Data API V3 - Error fetching video with google.youtube.videos.list()
  • Google drive API - Cannot read property 'OAuth2' of undefined
  • How to run a Google App Script using Google API Service Library (Node.js)

If this was not useful for your situation, I'm sorry.

Edit :

How about this modification? In my environment, I confirmed that this modification worked by googleapis of v30.0.0.

From :

drive.files.get({fileId: fileId, alt: 'media'})
.on('end', () => {
    console.log('Done');
})
.on('error', err => {
    console.log('Error', err);
})
.pipe(dest);

To :

drive.files.get({fileId: fileId, alt: 'media'}, {responseType: 'stream'},
    function(err, res){
        res.data
        .on('end', () => {
            console.log('Done');
        })
        .on('error', err => {
            console.log('Error', err);
        })
        .pipe(dest);
    }
);
like image 90
Tanaike Avatar answered Oct 13 '22 11:10

Tanaike