【发布时间】:2017-04-24 21:49:16
【问题描述】:
你好:我是 nodejs 和 mocha 的新手。我正在努力处理函数调用的返回值。即使(我认为)我已经适当地使用了回调 done(),它总是返回“未定义”。
在以下示例中,我如何确保 get() 的返回值始终返回正确的值而不是“未定义”。在这个函数中,我使用 requestJS 模块打开 google.com 并返回内容类型。但是,它目前返回 undefined。
非常感谢!
更新的帖子反馈:
- 包含
Test Case 3示例,以实现Callback。 结果是:我现在可以根据需要打印数据了。但是,我得到并错误告诉我调用 done()。我做错了吗?
在节点终端上运行的结果发布
suite
PRINT DATA: 200 text/html; charset=ISO-8859-1
√ Test case 1 (607ms)
undefined (<< ****** how to return the correct value?** )
PRINT DATA: 200 text/html; charset=ISO-8859-1
√ Test case 2 (603ms)
PRINT DATA: 200 text/html; charset=ISO-8859-1
√ Test case 3
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
...
Google.js
var request = require('request');
describe('suite', function(){
it('Tase case 1', function(done){
var options = { url: 'http://www.google.com', headers: {'Content-Type': 'text/html'}};
request.get(options, function (err, res, body){
console.log("PRINT DATA: " + res.statusCode + ' ' + res.headers['content-type']);
//do some stuff here
done();
});
});
it('Test case 2', function(done){
console.log(get(done));
});
it('Test Case 3', function(){
doCallback(callbackHandler);
});
});
function get(done){
var options = {
url: 'http://www.google.com',
headers: {'Content-Type': 'text/html'},
encoding: null
};
request.get(options, function(err, res, body){
console.log("PRINT DATA: " + res.statusCode + ' ' + res.headers['content-type']);
//do some stuff here
return done(), res.headers['content-type'];
});
}
function callbackHandler(data) {
console.log("PRINT DATA: " + data.statusCode + ' ' + data.headers['content-type']);
}
function doCallback(done){
var options = {url: 'http://www.google.com', headers: {'Content-Type': 'text/html'}, encoding: null};
request.get(options, function(err, res, body){
var finalData = res;
return done(finalData);
});
}
【问题讨论】:
-
欢迎来到 async 的美妙世界!你不能那样做;你需要使用回调或承诺。
-
感谢@SLaks 的指导。我现在在原始问题中添加了
callbacks的示例。请参考Test Case 3。我现在正在获取数据,但我收到一个错误,告诉我调用 done()。我不知道如何解决这个问题。也许我的方法是不正确的。请帮忙。 -
您将两个参数传递给一个只接受一个参数的函数。你希望它做什么?
-
@SLaks,我完全被卡住了。我也尝试删除第二个参数。问题似乎出现在
doCallback()内部,其中有对request.get(...)的调用。执行此操作的正确方法是什么?再次感谢。 -
你好@SLaks,它可能与节点有关吗?我正在使用
Node v7.9.0和requestjs v2.81.0。我使用node v 6.9.0在Run Kit 上尝试了它(在删除摩卡元素后),request.get(...)被正确调用。
标签: node.js mocha.js requestjs