0

I have a NIH API that can call only 50 pages at a time. The code below works for the first page of 50 items.

loadNIH() {
      let offset = 0;
      axios({
        method: "GET",
        url:
          "https://api.federalreporter.nih.gov/v1/projects/search?query=orgName:Medical University of South Carolina$fy:2016$&offset=" + offset +"&limit=50"
      })
        .then(res => {
          this.NIHData = res.data.items;
        })
        .catch(function(error) {
          console.log(error);
        });
    },

I need to loop all of the pages until I get all of the data. Each time the offset needs to increase by the number of pages received (requested 50) 9 pages in this call. I need it to ADD data to the array of this.NIHData. I got one working so I need some help creating the loop.

Thanks in advance

2
  • Couldn't you just move the offset out of your function, use offset += 50 inside the function, and call loadNIH() from inside the success path? (Until a condition is met) Commented Jul 24, 2019 at 15:32
  • Thanks for the suggestion but the problem is how many times to call it? The answer is on the first result set and you don't know when to quit until the last one.The number 9 changes based upon the query. Commented Jul 25, 2019 at 11:51

1 Answer 1

1

You should repeatedly call the API until you get an empty resultset. This can be most easily achieved with the help of setTimeout()

loadNIH() {
  let params =
  {
    offset: 0
  }
  this.NIHData = [];
  this.fetchPages(params);
},
fetchPages(args)
{
  axios.get("https://api.federalreporter.nih.gov/v1/projects/search?query=orgName:Medical University of South Carolina$fy:2016$&offset=" + args.offset +"&limit=50"
  )
    .then(res => {
      this.NIHData.push(res.data.items);
      args.offset += res.data.items.length;
      if (res.data.items.length > 0) setTimeout(this.fetchPages, 2);
    })
    .catch(function(error) {
      console.log(error);
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

Works great up until the this.fetchPages from fetchPages then axios throws an error saying Uncaught TypeError: Cannot read property 'offset' of undefined
Added setTimeout(this.fetchPages(args), 2); and it loops great, but now I get an error at this.NIHData.push(res.data.items); of Cannot read property 'NIHData' of undefined"
but if I change it to this.NIHData = res.data.items; I see the data load each page until the null response.
but this.NIHData = this.NIHData.concat(res.data.items); worked fine

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.