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

问题描述:

这是我的代码,它的所有工作都是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'); 
    } 
    }); 
}); 
+0

这会行得通,但我不需要打破产品的流动,尽管我对node.js是新手,所以我不知道我能做什么。我发布了调用这个的代码,只是为了获得更多的上下文。你的解决方案可能仍然有帮助,我只是不知道有关节点发布。 – 2012-07-13 16:47:28

+0

我编辑过,请检查。你需要在使用nodejs异步使用之前阅读如何使用回调代码。 – 2012-07-13 17:41:17

+0

所以我刚刚提交了一些代码,看起来几乎与上面的代码完全相同,并且工作完美。在您让我进入正确的思维模式之前,您的帮助感谢您确认我的思维过程。谢谢。 – 2012-07-13 19:37:50