Mar 28, 2016 12:47 PM
Hi there! I’m getting a bunch of records using the API and I would like to paginate the results - in the end, I’d like to display to the user 10 records on a page, then when they hit ‘Next Page’ I can show them the next 10, and so on, requesting the next 10 from the server each time (as opposed to grabbing all the records myself and paginating them in the client).
I see that there is eachPage() and fetchNextPage(), but I may be confused about how they work. I use eachPage to get the records and massage the data I get by looping through the records. However, the done() function doesn’t seem to get called unless I call fetchNextPage() and I want to call fetchNextPage() after a user chooses to get to the next page of results. Any help/advice would be awesome!
Mar 28, 2016 01:32 PM
If you’re having a single page app, you can call “fetchNextPage” when the user clicks on the “next” link. The done callback would not be called unless the user clicked through all the pages. That’s actually perfectly fine.
Mar 28, 2016 01:42 PM
Ah ok, I think i understand it more - i had a callback in the done() that was necessary to render the information, but I moved that callback into the eachPage() itself (after looping through the records) and that seems fine.
Now I have two more questions :slightly_smiling_face:
Mar 28, 2016 03:15 PM
There’s isn’t a previousPage - you could just memoize those once your client fetches it. fetchNextPage is not a class member - it’s a helper that’s passed into a page callback.
Here’s exactly where it’s created https://github.com/Airtable/airtable.js/blob/2dad5fe2b5d7551fe67971c67fd7ad1be0a5f80f/lib/query.js#L...
Mar 29, 2016 06:24 PM
Ah ok - I can add memoization to save the previous results. However, I think I’m in a bind calling fetchNextPage(). My airtable has a couple of tables where the records of one are needed to fetch the records of another. Using async.waterfall, I’m calling 3 tables, using the results from the previous table to get more records needed.
The first stage of the waterfall, where I call select().eachPage() for many records, I was calling the waterfallCallback() in the airtable’s select().done() (which was called after reaching the last page of records). I can move the waterfallCallback() into the .eachPage() so I can get all the information I need for the next waterfall stage, but then I cannot call fetchNextPage() because it would call the same waterfallCallback().
Is there an offset that can be retrieved from getting a page, or can an offset or pageNumber be passed to a request?
Mar 30, 2016 07:12 PM
You may be better off using the lower level API. Take a look at
https://github.com/Airtable/airtable.js/blob/2dad5fe2b5d7551fe67971c67fd7ad1be0a5f80f/lib/table.js#L... . As it’s a private method, it may be refactored, but if you have a strong use case, we can consider making it public.
With that, you can even store the offsets separately and fetch pages on demand.
Note, the offsets should be valid for a while, but not indefinitely - hours, not days. That may break some of the single page app logic.
Apr 28, 2019 02:11 PM
Please add the offset property in Node.js, That would be amazing!
Apr 28, 2019 07:17 PM
You can follow this article @Weiwei_Li
Airtable’s API is great and so is their documentation. However, I came across a small stumbling block while using it to build a simple project. The problem, Airtable returns a maximum of 100 records…
Apr 28, 2019 07:56 PM
Ah yes, I found that. It will return paginated data, but it seems to me like it still requires you to retrieve every single record of the table at the get-go.
So if you have 5000 records, it will need to keep calling fetchNextPage() until all 5000 records are retrieved. Only then is the data being paginated. So it doesn’t seem to be a very scalable solution.
I think my understanding is correct here?
IF we could access the offset property (which is available in curl), we could retrieve the first 100 records. Then, when an event is passed in to retrieve the next set of 100 records, the offset property could help us get those records by starting the record retrieval at the 101th record.
May 22, 2019 01:54 PM
You may be right that it could have a performance hit for say 5000 records but the Airtable node client does implement caching so that could help with the performance.
It’s never been slow in my experience to retrieve all the records but I have not tested with a base of 1000+ records.
I too was looking for the offset parameter but it has not been implemented yet to my knowledge.
May 22, 2019 04:37 PM
thanks for the reply! i didn’t know that caching was implemented.
You are correct that the offset property hasn’t been implemented for node.js, but I don’t think it is actually required based on Evan’s response here:
Hi, I would like to use the offset property to paginate through records with Node.js. I understand it exists for curl,...
also, thanks for the great articles you have written on your blog. they have been very helpful.
May 23, 2019 02:47 PM
I believe that I tried that approach, that is, trying to call fetchNextPage only when the user requires it but there are still challenges. In order to create the pagination markup that the tutorial is about you need to know the total number of records and pages and current page. You can’t do that when eachPage only gives you the first 100 but doesn’t tell you the total records.
And you can’t call fetchNextPage outside of the eachPage function so getting counts and current page in conjunction with calling fetchNextPage on the fly doesn’t seem possible but I could be wrong.
If you’re using the NodeJs client and found a better way, do share.
Also like he say Airtable isn’t for backend use per say and I was going to point that out when you mentioned 5000 record. If you app reaches that, it’s probably time to scale to firebase or mongodb. I just use Airtable for smaller projects.