これは以下の私のコードです。 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');
}
});