Google drive API downloading file. Error: Cannot read property 'data' of undefined

405 views
Skip to first unread message

Shivani Shinde

unread,
Nov 22, 2019, 7:05:31 AM11/22/19
to Google Apps Script Community
Hi,
I have followed the official docs for nodejs with Drive API v3.
I have following code for downloading files from google drive. 

function downloadFile(auth)
{
     const drive = google.drive({ version: 'v3', auth });
    //    drive.files.get({ fileId: fileId, fields: '*' }, (err, res) => {
    //        if (err) return console.log('The API returned an error: ' + err);
    //        console.log(res.data);
    //    });
         
    var fileId = '1RDrrFGV2fM7jvnxGFileId';
    var dest = fs.createWriteStream('./samples');

    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);
    }
    );
}

But I am not able to understand why is following error occurring:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'data' of undefined
    at /home/sshinde/workspace/node-sample-apps-script/index.js:157:13
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:546) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:546) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Googling the error isn't helping.

Any help will be appreciated!

Yasir Karam

unread,
Nov 24, 2019, 6:46:07 AM11/24/19
to google-apps-sc...@googlegroups.com
drive.files.get(
        {fileId: fileId, alt: 'media',}, 
        {responseType: 'stream'}, (err, { data }) => {
            if (err) {
                returnData.push(["ERR"]);
                returnData.push("" + err);
            } else {
                data.pipe(file);
                returnData.push("Downloaded");
            }
            callback(returnData);
        });


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/cae0a029-899f-40d1-b130-99e2b21e1083%40googlegroups.com.

Shivani Shinde

unread,
Nov 24, 2019, 11:25:49 PM11/24/19
to Google Apps Script Community
Hi Yasir. Thank you for resonding. 

Although I get following error for line :
      {responseType: 'stream'}, (err, { data }) => {

Error:
(node:7580) UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property `data` of 'undefined' or 'null'.



On Sunday, November 24, 2019 at 5:16:07 PM UTC+5:30, Yasir Karam wrote:
drive.files.get(
        {fileId: fileId, alt: 'media',}, 
        {responseType: 'stream'}, (err, { data }) => {
            if (err) {
                returnData.push(["ERR"]);
                returnData.push("" + err);
            } else {
                data.pipe(file);
                returnData.push("Downloaded");
            }
            callback(returnData);
        });


To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

Yasir Karam

unread,
Nov 25, 2019, 3:10:55 AM11/25/19
to google-apps-sc...@googlegroups.com
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.requesthere you can find all options

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/1ec2fcfa-4e87-4fa9-a775-a69fcaa3809d%40googlegroups.com.

Yasir Karam

unread,
Nov 25, 2019, 3:12:50 AM11/25/19
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages