Hi everyone!
It seems to be a common request on this forum, but I'd like to return all results for a cursor-based paginated API. The thread here: Returning all results for a cursor-based paginated API seems to be the best bet, but I can't seem to get it working on my app.
I've created two queries:
-
API Query to fetch my first
n
results
-
JavaScript Query, which calls the API Query as many times as we need to. In this case, we're using
pagination_token
as our cursor.
// Cursor-based pagination. This function recursively calls the API as long as the cursor from the previous request exists.
const fetchAll = (pagination_token, records) => {
// Base case: we've reached the end, and there are no more cursors.
if (pagination_token == null) return records
// Wrap the query result in a promise
return new Promise(resolve => {
return query9.trigger({
additionalScope: {
pagination_token
},
onSuccess: queryResult => {
// Add the records from this query to all the records we have so far
const newResults = records.concat(queryResult.records)
return resolve(fetchAll(queryResult.pagination_token, newResults))
}
})
})
}
return fetchAll(0, [])
Unfortunately, when I run the above JS query, my program gets stuck in an infinite loop. Could someone help debug?
Thank you!