Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我会变得这么奇怪;无法设置未定义的属性";在node.js中_Javascript_Node.js - Fatal编程技术网

Javascript 为什么我会变得这么奇怪;无法设置未定义的属性";在node.js中

Javascript 为什么我会变得这么奇怪;无法设置未定义的属性";在node.js中,javascript,node.js,Javascript,Node.js,我有一个非常简单的nodeunit测试,就像这样: 'use strict'; var controllerUtils = require('../lib/controllerUtils'); function Mock() { this.req = { headers: {} }; this.res = { }; this.nextWasCalled = false; this.next = function() { this.nextWas

我有一个非常简单的nodeunit测试,就像这样:

'use strict';

var controllerUtils = require('../lib/controllerUtils');

function Mock() {
    this.req = { headers: {} };
    this.res = { };
    this.nextWasCalled = false;
    this.next = function() {
        this.nextWasCalled = true;
    };
}

module.exports = {
    'acceptJson': {
        'Verify proper headers were set': function(test) {
            var mock = new Mock();
            controllerUtils.acceptJson(mock.req, mock.res, mock.next);

            test.ok(mock.nextWasCalled);
            test.equal(mock.req.headers.accept, 'application/json');
            test.equal(mock.res.lean, true);
            test.done();
        }
    }
}
但是当我调用controllerUtils.acceptJson时,我得到了错误
TypeError:无法设置未定义的属性“nextwascaled”

因此,我在chrome的控制台和节点命令行上对其进行了测试,测试是:

function Mock() {
    this.req = { headers: {} };
    this.res = { };
    this.nextWasCalled = false;
    this.next = function() {
        this.nextWasCalled = true;
    };
}

var m = new Mock();
console.log(m.nextWasCalled); //logs false
m.next();
console.log(m.nextWasCalled); //logs true
我不明白为什么我的代码不起作用,因为这是一个非常琐碎的代码,它在chrome的控制台和节点命令行上都非常有效

注:controllerUtils.acceptJson上的代码

module.exports.acceptJson = function(req, res, next) {
    req.headers.accept = 'application/json';
    res.lean = true;

    next();
};

controllerUtils.acceptJson
获取函数的引用作为参数。它不知道应该在哪个上下文中调用该函数,因此在没有任何上下文的情况下调用它

您的
next
方法要求上下文是定义它的对象。有两种方法可以修复代码:

将函数作为参数传递时将其绑定到上下文:

controllerUtils.acceptJson(mock.req, mock.res, mock.next.bind(mock));
定义函数时将其绑定到上下文:

this.next = function() {
    this.nextWasCalled = true;
}.bind(this);

controllerUtils.acceptJson
获取函数的引用作为参数。它不知道应该在哪个上下文中调用该函数,因此在没有任何上下文的情况下调用它

您的
next
方法要求上下文是定义它的对象。有两种方法可以修复代码:

将函数作为参数传递时将其绑定到上下文:

controllerUtils.acceptJson(mock.req, mock.res, mock.next.bind(mock));
定义函数时将其绑定到上下文:

this.next = function() {
    this.nextWasCalled = true;
}.bind(this);

controllerUtils.acceptJson
获取函数的引用作为参数。它不知道应该在哪个上下文中调用该函数,因此在没有任何上下文的情况下调用它

您的
next
方法要求上下文是定义它的对象。有两种方法可以修复代码:

将函数作为参数传递时将其绑定到上下文:

controllerUtils.acceptJson(mock.req, mock.res, mock.next.bind(mock));
定义函数时将其绑定到上下文:

this.next = function() {
    this.nextWasCalled = true;
}.bind(this);

controllerUtils.acceptJson
获取函数的引用作为参数。它不知道应该在哪个上下文中调用该函数,因此在没有任何上下文的情况下调用它

您的
next
方法要求上下文是定义它的对象。有两种方法可以修复代码:

将函数作为参数传递时将其绑定到上下文:

controllerUtils.acceptJson(mock.req, mock.res, mock.next.bind(mock));
定义函数时将其绑定到上下文:

this.next = function() {
    this.nextWasCalled = true;
}.bind(this);

您是否尝试设置下一次回调的上下文?类似于:controllerUtils.acceptJson(mock.req、mock.res、mock.next.bind(mock));(编辑)Woops,刚刚发布了一个答案。您是否尝试设置下一次回调的上下文?类似于:controllerUtils.acceptJson(mock.req、mock.res、mock.next.bind(mock));(编辑)Woops,刚刚发布了一个答案。您是否尝试设置下一次回调的上下文?类似于:controllerUtils.acceptJson(mock.req、mock.res、mock.next.bind(mock));(编辑)Woops,刚刚发布了一个答案。您是否尝试设置下一次回调的上下文?类似于:controllerUtils.acceptJson(mock.req、mock.res、mock.next.bind(mock));(编辑)Woops,刚刚发布了一个答案。