Search code examples
node.jsodata

Get all data from rest api (odata) response with node recursively via @odata.nextLink


I am getting an api response where 50000+ products are fed back at 100 at a time. At the end of the response, there is '@odata.nextLink'. Can one automate fetching the remaining data with node using the '@odata.nextLink'?

I have tried a while loop around the request but no success.

ie. while(body['@odata.nextLink']){ request... }

Can it be done?


Solution

  • Try something that looks like this:

    const fakeAPI = async id => (
      id==10
      ? { id }
      : { id, nextId: id+1 }
    );
    
    ( async () => {
      let allResponses = [];
      let finished = false;
      let id = 0;
      while (!finished) {
        let response = await fakeAPI(id);
        finished = response.nextId===undefined;
        id = response.nextId;
        allResponses.push(response);
      }
      console.log(allResponses);
    })()

    Using async/await is pretty cool when looping with asynchronous code.