我尝试使用require.js为我的node.js服务器编写一个模块,该模块刚刚返回我想从url获取的对象。但不知何故,我不能返回我的方法得到的值。 http.get ...在我返回值之后执行,以便我只是得到“undefined”,但为什么?无法通过Require.js调用函数
你能帮我吗?对不起,如果这是一个愚蠢的问题,但我真的很新的javascript node.js和require.js。
define(['http'], function(http){
console.log('Hi ich bin pullArchiveVolume');
var response = 1;
console.log('Log 1: ' + response);
http.get("http:...", function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
console.log("Log 2: " + response);
response = 2;
console.log("Log 3: " + response);
response = JSON.parse(body);
return response;
// console.log("Log 2 :", response);
// console.log("Got response: ", response);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
console.log("Log 4: " + response);
return response;
})
控制台输出:
Hi ich bin pullArchiveVolume
Log 1: 1
log 4: 1
log 2: 1
log 3: 2
谢谢!
欢迎来到** async **的美妙世界!你需要使用回调。 – SLaks