Request paginated data from Notion API with Next.js

Jon Meyers
InstructorJon Meyers
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

In this video, we learn how to implement cursor-based pagination in our Next.js application, to request all movies from the Notion Database.

By default, Notion will return 100 results per request to the API. Therefore, we need to continue fetching results until we have the full set. This can be done using a while loop that checks the has_more field, returned by Notion's API, to determine whether we have fetched every movie.

If the has_more property is true, Notion will also provide a cursor value to fetch the next 100 results. When the has_more field is set to false, we have retrieved all movies from the Database, and can stop looping.

Instructor: [0:00] Our application appears to be working correctly, but if we console log out the length of our array, we'll see that we're only getting 100 results.

[0:07] If we go back to our Notion document, we have 119 movies in our database. If we add a filter for just our unwatched movies, we should still be getting back 110 movies. If we were to pass through the original data that we got as props and then log that out in our component, we'll see the property hasMore is set to true and we have a value here for our next cursor.

[0:34] Notion's API is only going to give us back 100 results at a time. If we want to grab the next 100 results we need to use this cursor. We can do this back in our getStaticProps function by declaring a new variable for all of our results.

[0:48] We're using let here because we want to be able to change this value. We also want to change our data declaration to use let as well. Then after we've made our first request to Notion's API, we can set results to be equal to a new array where we're going to spread in data.results.

[1:07] We can then use a While loop to say, while data.hasMore is set to true, we want to make another request to Notion's API by specifying the start cursor to be that data.nextCursor value. We then want to override our results array with the current results plus the new data.results.

[1:36] We can then perform this map over our entire results array rather than the most recent request. Now, if we remove this extra data prop and do the same up in our component, we can just print out the length of our movies again.

[1:51] If we come over here and refresh our page, I'm getting a reference error here because I'm trying to reinitialize the variable data. In the While loop we just need to remove the keyword let. Now when we refresh our page, we see we have our full 110 results and our choose movie functionality is still working.

egghead
egghead
~ 12 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today