How can I get all data from a JSON api when pagination is enabled?

I need to get all data from this api: , but I'm getting the first page(page 1) data with this code:

urlApi = ' let repo = await axios.get(urlApi); repo.data.hits.map((data) => { console.log(data); }); 

This JSON contains 'nbPages': 50, But I need to map all 50 JSON pages.

Do you have any ideas for mapping?

0

2 Answers

You could use a while loop and just append the hits to an array until you've completed all requests:

let repo = null, page = 0, results = []; do { repo = await axios.get(`${urlApi}&page=${page++}`); results = results.concat(repo.data.hits); } while(repo.data.page < repo.data.nbPages) console.log(results); 
1

Due to the fact that the standard value for "hitsPerPage" is 20, you can set this value to get all of the hits at once by using this URL instead (20 hits * 50 pages = 1000):

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like