1

I'm teaching myself Nodejs and am trying to populate a page with a list of nearby businesses' opening and closing hours from Yelp's API. I created a POST route to my page in Express, calling the Yelp API using the Yelp Fusion client. I am able to gather an array of ids which must be used in another endpoint in order to fetch the hours of operation, however I keep receiving TOO_MANY_REQUESTS_PER_SECOND errors when do this, despite setting the limit in the request.

Server.js

var express = require("express");
var app = express();
var yelp = require("yelp-fusion");
var bodyParser = require("body-parser");

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
let client = yelp.client("API_HIDDEN");

app.get("/", function(req,res){
    res.render("landing");
});

///Initial request made to obtain business ids
app.post("/", function(req, res){
    client.search({
        term: 'cafe',
        location: 'Oakland',
        limit: 20
    }).then(response => {
        var ids = [];
        var businesses = response.jsonBody.businesses;
        var idName = businesses.map(el => {
            ids.push(el.id);
        });

        // request separate endpoint, passing ids from the ```ids```array
        for(var x = 0; x < businesses.length; x++){
            client.business(ids[x]).then(response => {
                console.log(response.jsonBody.hours);
            })}.

        res.render("search");
    }).catch(e => {
        console.log(e);
    });
})

app.listen(3000);

I have tried calling client.businesses[id] both inside and outside a for loop but this resulted in errors too. I am confused on the behaviour of this as I am only making 20 calls, far below the minimum, but also how to possibly pass the ids if not for an array as I have run out of ideas. Thank you in advance for your help.

brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
Pbculley
  • 79
  • 8

1 Answers1

2

Spread out the api calls over time.

var delay = 1.1 * 1000; // 1.1 seconds in milliseconds  
for(var x = 0; x < businesses.length; x++){
  setTimeout(function(i){
      client.business(ids[i]).then(response => {
      console.log(response.jsonBody.hours);
      });  
  },delay*x,x);
}
rckrd
  • 3,124
  • 1
  • 13
  • 23
  • Thank you kind stranger, that works perfectly. I had seen```setTimeout``` mentioned previously in a separate post, however could not figure out a way to implement it into the function. Thanks again. – Pbculley Dec 29 '18 at 21:39
  • One interesting thing to note, the ```setTimeout``` function will work correctly the first few times I start and restart the server but after that when I run I begin receiving ```TOO_MANY_REQUEST" errors again. I have adjusted the milliseconds of the delay down so I understand that it will cause 429 errors if I'm allotting less time for each call. I'm just curious as to why it will let you make the call the first few times (why the fluctuation?). – Pbculley Dec 29 '18 at 22:19