简体   繁体   English

从被调用函数的回调中访问函数变量

[英]Accessing a function variable from a called function's callback

This is my code below, it's all working with the exception of return_info not getting setup. 这是我的下面的代码,除了return_info未设置之外,所有代码都可以正常工作。 Is it possible to access the parent return_info variable from within the callback that is called by the request function? 是否可以从请求函数调用的回调中访问父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;
  }
}

EDIT: For Further reference this is the code that calls it. 编辑:作为进一步参考,这是调用它的代码。

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');
}
});

As request is asynchronous, it will execute after you have returned return_info . 由于请求是异步的,因此它将在您返回return_info之后执行。 So, you need to provide a callback to the function as show below 因此,您需要提供该函数的回调,如下所示

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);
});

EDIT - With the context 编辑-与上下文

You need to think along the callback ways if you want to start working with node.js, Below is the example on how you should do it. 如果要开始使用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');
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM