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.
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.
If this was not useful for your situation, I'm sorry.
How about this modification? In my environment, I confirmed that this modification worked by googleapis of v30.0.0.
drive.files.get({fileId: fileId, alt: 'media'})
.on('end', () => {
console.log('Done');
})
.on('error', err => {
console.log('Error', err);
})
.pipe(dest);
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);
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With