loopback.io错误:发送后无法设置标头。确认用户

时间:2015-03-16 11:45:40

标签: node.js mocha loopbackjs supertest

我使用mocha和supertest来创建功能测试。以下是测试的副本。

it("should verify user POST /api/User/confirm", function (done) {
    app.models.Users.findById(userId, function (err, usr) {
        json('get', '/api/user/confirm?uid=' + userId + '&token=' + usr.verificationToken)
        .expect(204,done);
    });
});

然而,此测试返回:

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
    at ServerResponse.res.setHeader (/node_modules/loopback-explorer/node_modules/express/node_modules/connect/lib/patch.js:134:22)
    at ServerResponse.header (/node_modules/loopback/node_modules/express/lib/response.js:700:10)
    at ServerResponse.send (/node_modules/loopback/node_modules/express/lib/response.js:154:12)
    at ServerResponse.json (/node_modules/loopback/node_modules/express/lib/response.js:240:15)
    at ServerResponse.send (/node_modules/loopback/node_modules/express/lib/response.js:142:21)
    at defaultHandler (/node_modules/loopback/node_modules/strong-remoting/lib/rest-adapter.js:379:11)
    at restErrorHandler (/node_modules/loopback/node_modules/strong-remoting/lib/rest-adapter.js:348:14)
    at Layer.handle_error (/node_modules/loopback/node_modules/express/lib/router/layer.js:58:5)
    at trim_prefix (/node_modules/loopback/node_modules/express/lib/router/index.js:300:13)
    at /node_modules/loopback/node_modules/express/lib/router/index.js:270:7
    at Function.proto.process_params (/node_modules/loopback/node_modules/express/lib/router/index.js:321:12)
    at next (/node_modules/loopback/node_modules/express/lib/router/index.js:261:10)
    at Layer.handle_error (/node_modules/loopback/node_modules/express/lib/router/layer.js:54:12)
    at trim_prefix (/node_modules/loopback/node_modules/express/lib/router/index.js:300:13)
    at /node_modules/loopback/node_modules/express/lib/router/index.js:270:7
    at Function.proto.process_params (/node_modules/loopback/node_modules/express/lib/router/index.js:321:12)
    at Object.next (/node_modules/loopback/node_modules/express/lib/router/index.js:261:10)
    at Object. (/node_modules/loopback/node_modules/express/lib/router/index.js:603:15)
    at Object. (/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31)
    at Object.immediate._onImmediate (timers.js:363:16)
    at processImmediate as _immediateCallback

此时我正在使用最新版本的环回。任何人都可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

这通常是由于代码中res.send()的多次使用造成的。我会检查是否是这种情况。