Javascript 从被调用函数访问函数变量';s回调

Javascript 从被调用函数访问函数变量';s回调,javascript,node.js,Javascript,Node.js,这是我下面的代码,除了return\u info没有设置外,所有代码都正常工作。是否可以从请求函数调用的回调中访问父return_info变量 module.exports = { fetch_template_by_id : function(id) { var request = require('request'); var return_info = {}; //TO BE MODIFIED request('http://localhost:5000/tem

这是我下面的代码,除了return\u info没有设置外,所有代码都正常工作。是否可以从请求函数调用的回调中访问父return_info变量

module.exports = {
  fetch_template_by_id : function(id) {
    var request = require('request');
    var return_info = {}; //TO BE MODIFIED
    request('http://localhost:5000/templates/v1/template/' + id, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log("Success - Error = " + error + " | Response = " + response + " | Body = " + body);
        return_info.body = body;                  // Should Modify the top
        return_info.json = JSON.parse(body);      // Should Modify the top
        return_info.success = true;              // Should Modify the top
      } else {
        console.error("There was an error requesting template ID=" + id)
        return_info.error = error;                // Should Modify the top
        return_info.response = response;          // Should Modify the top
        return_info.success = false;              // Should Modify the top
      }
    });
    return return_info;
  }
}
编辑:为了进一步参考,这是调用它的代码

app.get('/form', function (req, res) {
var template_helper = require('../services/template-helper');
var template = template_helper.fetch_template_by_id("BirthRecord");
console.log(template);
if (template.success === true) {
  res.render('form', {"template": template.json });
} else {
  res.render('index');
}
});

由于请求是异步的,它将在您返回
return\u info
后执行。因此,您需要提供一个回调函数,如下所示

module.exports = {
  fetch_template_by_id : function(id, cb) {
    var request = require('request');
    var return_info = {}; //TO BE MODIFIED
    request('http://localhost:5000/templates/v1/template/' + id, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log("Success - Error = " + error + " | Response = " + response + " | Body = " + body);
        return_info.body = body;                  // Should Modify the top
        return_info.json = JSON.parse(body);      // Should Modify the top
        return_info.success = true;              // Should Modify the top
      } else {
        console.error("There was an error requesting template ID=" + id)
        return_info.error = error;                // Should Modify the top
        return_info.response = response;          // Should Modify the top
        return_info.success = false;              // Should Modify the top
      }
      cb(return_info);
    });
  }
}

fetch_template_by_id('awesome', function (info) {
  console.log(info);
});
编辑-使用上下文

如果您想开始使用node.js,您需要考虑回调方法,下面是关于如何进行回调的示例

app.get('/form', function (req, res) {
  var template_helper = require('../services/template-helper');
  template_helper.fetch_template_by_id("BirthRecord", function (template) {
    console.log(template);
    if (template.success === true) {
      res.render('form', {"template": template.json });
    } else {
      res.render('index');
    }
  });
});

这会起作用,但我不需要尽可能破坏产品流程,尽管我对node.js还不熟悉,所以我不知道我能做什么。我发布了调用此函数的代码,只是为了让您能够获得更多上下文。您的解决方案可能仍有帮助,我只是对节点了解不够,无法在上面发布。我已编辑,请检查。在异步使用nodejs之前,您需要了解如何使用回调编写代码。因此,我刚刚提交了一些代码,这些代码看起来与上面的代码几乎相同,并且工作得非常好。之前你的帮助让我进入了正确的思维模式,谢谢你确认我的思维过程。谢谢