question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docs: Drive pagination code is incorrect

See original GitHub issue

API v3 https://developers.google.com/drive/api/v3/reference/files/list

Looking at the List documentation (Node at the very least), there is a lot of information that is left out which is fundamental to making paginated requests:

  • The pagination parameter is called nextPageToken, not pageToken as it lists. Even my linter tells me that what I’ve written is incorrect, but if I ignore the lint error and submit the request, it works. When using the ‘linter-correct’ version, the request fails.
  • It appears you must include the same ‘q’ parameter in your request as well
  • You must include fields: 'nextPageToken,files(id, name)' in the request, even though not including it returns a nextPageToken, it appears to be invalid

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
fhinkelcommented, Dec 7, 2020

@TaylorDale Thanks for the bug report. Looks like a duplicate to https://github.com/googleapis/google-api-nodejs-client/issues/1885

0reactions
TaylorDalecommented, Dec 12, 2020

Your implementation is dependant on your use case but if you wanted to pull all of the data when there is pagination you can do something like the following:

const generateRequest = (nextPageToken: string) => drive.files.list({ q: "", nextPageToken, pageSize: 10, fields: 'nextPageToken, files(id, name)' });

const data = [];
let token = undefined;

do {
  const req = generateRequest(token);
  const resp = await req.execute();

  data.push(data.files);
  token = data.nextPageToken;
} while (token)

This assumes that nextPageToken is undefined when pagination is finished, which may not be the case with Google APIs, there may be another field that indicates this is the last page of data so deal with it accordingly.

Please note though that is more psuedocode, you’ll need to use the correct Google API methods and more robust error checking is probably needed, but that would be how you’d deal with pagination and have all the data in the data variable.

You might have better luck with further help on Stack Overflow seeing as this isn’t related to the actual bug in the docs 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google drive pagination not working. Empty nextPageToken
Just found the solution: The nextPageToken must be added outside of the files() section. My mistake was that first I put it inside...
Read more >
Change a document's page setup: pages or pageless - Android
To change whether a document has pages or is pageless: On your Android phone or tablet, open the Google Docs app. Open a...
Read more >
Pagination with rel="next" and rel="prev" - Google Developers
When implemented incorrectly, such as omitting an expected rel="prev" or rel="next" ... and code samples are licensed under the Apache 2.0 License.
Read more >
SEO-Friendly Pagination: A Complete Best Practices Guide
Pagination Creates Thin Content. Correct if you have split an article or photo gallery across multiple pages (in order to drive ad revenue...
Read more >
Use start cursors and limits to paginate Firestore collections
Explore further. For detailed documentation that includes this code sample, see the following: Paginate data with query cursors · Paginating data with query ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found