我创建了下面的函数来给我一个随机的事实。顶部函数有效:
function getCatFact(callback) {
request('http://catfacts-api.appspot.com/api/facts', function (error, resp, body) {
if (!error && resp.statusCode == 200) {
var jsonObj = JSON.parse(body);
message = "Thanks for texting Cat Facts! Remember everytime you text you will receive an instant Cat Fact! "+ jsonObj.facts[0];
return callback(message);
}
});
}
用以下方法调用函数:
getCatFact(function(returned_message){
console.log(returned_message); // This returns the catfact fine
response = returned_message; // Response is not correctly being set.
});
当我调用此功能时。响应未定义。响应在文件顶部声明,只有var响应;然后我回复了。