Skip to content

Drive API: example mistake #1788

Closed
@ttv20

Description

@ttv20

On /samples/drive/export.js there is a mistake
I tried the function a few times and it downloaded 20kb less each time
So I discover that the function finish to run by await to res.data.on('end')
but the stream didn't finish to write the data to the fs yet
The correct function is:

async function runSample() {
  // [START main_body]
  const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
  const destPath = path.join(os.tmpdir(), 'important.pdf');
  const dest = fs.createWriteStream(destPath);
  const res = await drive.files.export(
    {fileId, mimeType: 'application/pdf'},
    {responseType: 'stream'}
  );
  res.data.pipe(dest)
  await new Promise((resolve, reject) => {
    dest
      .on('finish', () => {
        console.log(`Done downloading document: ${destPath}.`);
        resolve();
      })
      .on('error', err => {
        console.error('Error downloading document.');
        reject(err);
      })
  });
  // [END main_body]
}

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions