skip to Main Content

I’m getting records as a JSON object from an API with a limitation of 200 records. I need to get from 800 to 1200 records, depending on the day.

This (below) kind of works except, for obvious reasons, it’s grabbing the same 200 records with each iteration. I end up with one JSON object with 1200 records but records 1, 201, 401, 601 etc. are all the same.
I feel like a counter is needed here but I can’t figure out how to implement it.
Any help or pointed in the right direction is appreciated.
Thank you!

const iteration = new Array(0, 1, 2, 3, 4, 5);
const dealRecords = new Array();

for (var i = 0; i < iteration.length; i++) {
  SOMECOMPANY.API.searchRecord({
    Entity: "Greeting",
    Type: "criteria",
    Query: "(Text:equals:Hay)",
  }).then(function (data) {
    dealRecords.push({ data });
  });
}
console.log(dealRecords);

2

Answers


  1. You will need to offset the results you are getting by using an offset or pagination parameter (or anything similar).

    The exact way of doing this will depend on the API you are using. Twitter for example provides you with a pagiantion token, while others might expose a query param to offset the results.

    Login or Signup to reply.
  2. According to the API doc you can insert a page param after the config object. And I think you won’t need an array for the loop

    Like:

    for (var page = 1; page < 7; page++) {
                SOMECOMPANY.API.searchRecord({ Entity: "Greeting", Type: "criteria", Query: "(Text:equals:Hay)" }, page)
                   ...
            }
    

    perhaps with page.toString() because the page param has to be a string.

    Or you can try to change the perPage param and get just one page with all you need.

    SOMECOMPANY.API.searchRecord({ Entity: "Greeting", Type: "criteria", Query: "(Text:equals:Hay)" }, "1", "1200")
    `` 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search