Node.js回调函数问题

时间:2016-06-02 03:38:59

标签: node.js testing callback mocha chai

我使用mocha和请求模块进行一些测试。

为什么这个.restresponse ='测试'从来没有身体json 使用下面的代码。 mocha测试调用下面的函数代码,然后在回调上检查this.restresponse。它始终是测试'即使身体在回调中被传回。

this.restresponse = 'test';
request(this.formatted, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        this.restresponse = body;
        callback(null, body); // pass back the body
    } else {
        this.restresponse = error;
        callback(error, null); // pass back the error
    }
});

非常感谢

1 个答案:

答案 0 :(得分:-1)

因为请求方法无法访问此对象。杜!