0

Okay, so my code is pulling data from a yelp business using their official API. My problem is that I can't seem to get the data to return out of the function. The problem isn't in ejs, it's that the data doesn't return when I tell it to! I just get undefined with some attempts, and with others (including the one I'm going to show here), I get an empty array. I'm pasting only the code that's important, let me know if you need more!

 function yelp(){

  var b = [];
  var i = 0;
 (struck the initialization of client)
client.business("(struck)", function(error, data) {
      if (error != undefined){
        res.send("an error occured. exiting");
        process.process.reallyExit();
      }
      b[i++] = data.name;
      b[i++] = data.display_phone;
      b[i++] = data.rating;
      console.log(b); //NEW!!
    });
    console.log(b);
    return b;
}

app.get('/yelp', function(req,res){
     var arr = yelp();
     console.log(arr);
     res.render('yelp.ejs', {title: 'Yelp!', arr: arr});   
  });
}

I added one more line of code, that I THINK may have narrowed down the problem to being related to my poor internet connection. I added ANOTHER console.log(b), this time inside of the business API call. the console.log(arr) is shows second, the console.log(b); just before the reutrn shows first, and LAST is the console.log(b) INSIDE the API call. It also took a good 30 seconds for that log to appear, and it appeared AFTER the page loaded. So, how do I go about making the page wait for the data? Or is this unrelated to my problem?

  • 2
    possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Ben Fortune Mar 23 '15 at 16:14

3 Answers3

1

Without knowing their API, I do recognize the callback style. The result of the call to your client would then be in the data parameter in the callback, and thats where you want to render your view.

The following is not tested.

function yelp(cb) {
    var b = [];
    var i = 0;
    // (struck the initialization of client)
    client.business("(struck)", function(error, data) {
        if (error) {
            return cb(error);
        }
        b[i++] = data.name;
        b[i++] = data.display_phone;
        b[i++] = data.rating;
    });
    console.log(b);
    cb(null, b)
}

app.get('/yelp', function(req, res) {
    yelp(function(err, arr) {
        if (err) {
            res.send("an error occured. exiting");
            process.process.reallyExit();
            return;
        }
        console.log(arr);
        res.render('yelp.ejs', {
            title: 'Yelp!',
            arr: arr
        });
    });

});

The thing to notice here, is the callback passing. This is normally how you do async work in Node.js. It can be made a bit prettier using promises.

Jeff
  • 12,085
  • 12
  • 82
  • 152
0

nodejs is async, meaning the app won't wait for the yelp() function to return. you can pass the yelp function a callback like so:

 function yelp(callback){

  var b = [];
  var i = 0;
 (struck the initialization of client)
client.business("(struck)", function(error, data) {
      if (error != undefined){
        res.send("an error occured. exiting");
        process.process.reallyExit();
      }
      b[i++] = data.name;
      b[i++] = data.display_phone;
      b[i++] = data.rating;
    });
    console.log(b);
    callback(b);
}
yelp(funtion(arr) {
 res.render('yelp.ejs', {title: 'Yelp!', arr: arr});   
})
0

You are expecting sync things to happen, but it's async. Your client.business method takes in a callback as it's second argument which isn't returning by the time res.render gets called.

Try this:

function yelp(callback) {

  var b = [];
  var i = 0;

  client.business("(struck)", function(error, data) {
    if (error != undefined){
      res.send("an error occured. exiting");
      process.process.reallyExit();
    }
    b[i++] = data.name;
    b[i++] = data.display_phone;
    b[i++] = data.rating;

    // No returns in async.  Just call the callback.
    callback('yelp.ejs', { {title: 'Yelp!', arr: b}})

  });

}

app.get('/yelp', function(req,res){
  yelp(res.render);
});
Levi Mootz
  • 306
  • 1
  • 4