Reputation: 1
I want to make a change in my script.js file to be able to concatenate results upto atleast 10 pages called from the api. Please refer to line 10 below - results=results.concat('&page=1');
const loc = document.getElementById("locationcity").value;
const start = document.getElementById("startdate").value;
const end = document.getElementById("enddate").value;
var results = baseUrl + '/location?name=';
results=results.concat(loc);
results=results.concat('&minDate=');
results=results.concat(start);
results=results.concat('&maxDate=');
results=results.concat(end);
results=results.concat('&page=1');
const url=results;
const res = await fetch(url);
const record = await res.json();
When i change the code to results=results.concat('&page=10') or results=results.concat('&page=6'); it will show me what is on that page but i want to know how to call a range eg. from page 1 to 10. Have tried & and + but it isn't working.
Upvotes: 0
Views: 298
Reputation: 545
Without looking into the API documentation we can just guess.
It might be that the API does offer a feature like &page=1&page=2&page=3...
to concatenate all pages to a single response but it is unlikely since the reason for pagination is to page over the results. So I guess you can only send a single page and as you noticed already the API seems to chose the last page
parameter that you send, ignoring the others.
If my guess is correct, you need to do 10
requests to get 10
pages.
const loc = document.getElementById("locationcity").value;
const start = document.getElementById("startdate").value;
const end = document.getElementById("enddate").value;
var records = new Array();
for (var i = 0; i < 10; i++) {
var results = baseUrl + '/location?name=';
const url = results
.concat(loc)
.concat('&minDate=')
.concat(start)
.concat('&maxDate=')
.concat(end)
.concat('&page=')
.concat(i);
const res = await fetch(url);
const record = await res.json();
records.push(record);
}
Then you can do something with all the results in that array to combine them in a single result, like concat existing array, flatten or something, depending on how your data (the jsons) look like.
Upvotes: 1