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;
});
});
}
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
None have a response?
I have the same problem.
@JustinBeckwith Any insights? Appears I'm not alone with this.
Help ..... same issue !
There is some problem with documentation. The export function return this.
https://github.com/google/google-api-nodejs-client/blob/32c6cca65683984937ef0a05e5077cab472f42cd/src/apis/drive/v3.ts#L577-L598
but if we looking for what that function return
https://github.com/google/google-api-nodejs-client/blob/32c6cca65683984937ef0a05e5077cab472f42cd/src/lib/apirequest.ts#L201-L211
nothing is returned, so we need to pass a callback and read the result.data
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
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
Apologies for the problems folks. We're tracking this over in #1024 right now. The good news is that we've identified the issue, and you can see an updated example in #1028.
Hello guys.. I got same error, so I came here.. @SergioDonati you wrote that your code in this comment works, but I am getting error:
Error: Required parameter: mimeType
at RequestError.Error (native)
at new RequestError (/path/node_modules/google-auth-library/lib/transporters.js:34:42)
at Request._callback (/path/node_modules/google-auth-library/lib/transporters.js:96:27)
at Request.self.callback (/path/node_modules/request/request.js:186:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/path/node_modules/request/request.js:1163:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/path/node_modules/request/request.js:1085:12)
even the mimeType is provided :/
@SergioDonati Thank you so much for your fix. It saved me so much time
Thanks @SergioDonati. However @JustinBeckwith typescript raises an error on Sergio's code sample
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
});
@jleppert could you provide a link to the specific documentation that hasn't been updated, and perhaps open a new issue?
It looks like @JustinBeckwith updated the example in #1028 quite some time ago; I'm wondering what we've missed updating?
Here is one if the docs pages that need the fix in their examples
https://developers.google.com/drive/api/v3/manage-downloads
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
I know this isn't awesome, but I would really trust the samples in this repository a little more than what you find on developers.google.com. The content on the site isn't always exactly up to date, but I can promise the samples in this repo are tested against master.
Most helpful comment
finally i made a export script working for me, i hope help someone else;
is important the
responseType: "stream"
to have a stream inresponse.data
.options are passed to
axios.request
, here you can find all options