1

New to the MEAN stack, trying to make an API response I have properly available to the res.render jade template below.

exports.jap = function(req, res){
  var client = new BasicAuth();
  client.authenticate(function(err, response, body, cookie) {
    var client = new Api('sample.com', cookie);
    client.getProducts(function(err, response, body) {
        console.log(JSON.stringify(body['result']));
        var result = body['result'];
        console.log(result);
    });
  });

  res.render('lang', {
      title: "灾情 (Mandarin)",
      reqFor: "申请人",
      product: "产品",
      problem: "问题",
      results: result // I would like my result var from above accessable here
  });
};

Right now I'm getting result is not defined

Brian Smith
  • 117
  • 8
  • @unlucky13 This looks like an issue trying to use a variable that gets its value asynchronously, whereas OP is acting as if it was synchronous—not sure if this is best marked as a dupe for "scoping" – Nick Zuber Oct 03 '16 at 04:43
  • ahh, so would i want to require the async package and set that up? – Brian Smith Oct 03 '16 at 04:51
  • @NickZuber Well, it's not (a)synchronicity-related question. See the `var result` line. He clearly doesn't know how scoping works in JavaScript. – Ram Oct 03 '16 at 04:51
  • @ unlucky13 Good point, but it's still an asynchronous/synchronous issue _even_ if he fixes the scope. @BrianSmith you should first read the question this was marked as a dupe of, then when you understand scoping check out http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function?noredirect=1&lq=1 – Nick Zuber Oct 03 '16 at 04:56

0 Answers0