nodejs,函数调用即使完成后也不会返回未定义



你好:我是Nodejs和Mocha的新手。我正在努力从功能调用中的返回值。即使(我认为(我已经适当地使用了回调。

,它总是返回"未定义"。

在下面的示例中,如何确保从get((中的返回值始终返回正确的值,而不是"未定义"。在此功能中,我使用了请求JS模块打开Google.com并返回内容类型。但是,它目前返回未定义的。

非常感谢!

更新了做出的后反馈:

  • 包括Test Case 3示例,以实现Callback结果是:我现在能够根据需要打印数据。但是,我会出现错误,告诉我打电话完成((。我在做什么不正确吗?

在节点终端上运行的结果帖子

 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); 
    }); 
}

一种可能的解决方案(使用回调(

var request = require('request');
describe('suite', function(){
    it('Test Case 3', function(done){
        doCallback(callbackHandler, done);  
    });
    it('Test Case: 3A', function(done){
        doCallback(function(data){
        console.log("PRINT DATA: " + data.statusCode + ' ' + data.headers['content-type']);  
        done();        
    });  
});     
});
function callbackHandler(data, done) {
    console.log("PRINT DATA: " + data.statusCode + ' ' + data.headers['content-type']);  
    done();
}
function doCallback(callback, done){
    var options = {url: 'http://www.google.com', headers: {'Content-Type': 'text/html'}, encoding: null};     
    request.get(options, function(err, res, body){           
        return callback(res, done); 
    }); 
}

一个可能的解决方案(使用Promos(

var request = require('request');
describe('suite', function () {
    this.timeout(10000);
    it('Test Case 3', function () {
        return doCallback()
            .then(function (res) {
                console.log(res.statusCode + " " + res.headers['content-type']);
            })
            .catch(function(res){
                console.log(res);
            });
    }); });
function doCallback() {
    return new Promise(
        function (resolve, reject) {
            var options = { url: 'http://www.google.com', headers: { 'Content-Type': 'text/html' }, encoding: null };
            request.get(options, function (err, res, body) {
                if (err)
                    reject(err); 
                else 
                    resolve(res);
            });
        }
    ); }

最新更新