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

标签 javascript node.js

这是我下面的代码,除了 return_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_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');
    }
  });
});

关于javascript - 从被调用函数的回调访问函数变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11474591/

相关文章:

javascript - 如何在使用哈希路由器的 React 应用程序中实现 Microsoft Oauth

javascript - Ember.js 对大十进制数进行四舍五入

javascript - 清除主干模型上除一个属性外的所有属性

javascript - AngularJS - 如何使用模型中的数据来创建图表

javascript - Nodejs 中 yEnc 的错误处理

node.js - 如何解决 MalformedResponse 'final_response' 必须设置。 Action 模拟器错误

node.js - Heroku 上的 Node 和 Redis 应用程序错误

php - Javascript 相当于 PHP 的 md5() ,它也适用于多字节字符串?

javascript - 将一个对象数组与另一个对象数组进行比较

mysql - 在 Express.js 应用程序中处理 Mysql 数据库的最佳方法是什么?