Hello!
Just a heads up, I am still very new to javascript and retool.
My goal is to use the Scopus API to retreive data. The issue being that I could only retrieve 25 results at once. I used the cursor parameter to get through the pagination. I found the following on Stackoverflow
The basic idea behind this pagination (that's what you're after) is:
- Run a query with unlimited number of results with
cursor
set to "*"- Set
start
to 0 and get the firstcount
results- Set
start
tostart+count+1
and get the nextcount
results- Repeat step 3 until all results are fetched
I found some code in this forum and tried to change it to fit the description above as follows:
const fetchAll = (start, records, count) => {
// Base case: if no more records are returned
if (records.length < count) return records; // Or some other stopping condition
// Wrap the query result in a promise
return new Promise(resolve => {
return searchScopus.trigger({
additionalScope: {
cursor: "*", // Use "*" as the cursor to start from the beginning
start: start, // Start from the current position (start)
count: count // How many results to fetch
},
onSuccess: queryResult => {
// Add the records from this query to the accumulated results
const newResults = records.concat(queryResult.records);
// Update start for the next API call: start + count + 1
const newStart = start + count + 1;
// Recursively call fetchAll to continue fetching the next batch of records
return resolve(fetchAll(newStart, newResults, count));
}
});
});
};
// Start the process with start = 0 and specify count (e.g., 100 results per page)
return fetchAll(0, [], 25); // Adjust count as needed
My initial thought was that Ill just put the cursor variable as input for the header:
yet I get an error. I assume I am misunderstanding something here. Can anyone help me understand?
Thank you in advance!