我正在尝试执行登录功能但Error: Can't set headers after they are sent
会一直显示在我的终端中,根据我的理解,此错误是由res
引起的,但我找不到错误我的代码..
这就是我到目前为止所做的:
this.handleLoginRequest = function(req, res, next) {
"use strict";
var data = req.body;
users.validateLogin(data, function(err, user) {
"use strict";
if(err) {
res.send(400, err.message);
}else{
sessions.startSession(data, function(err, session_id) {
"use strict";
if (err) return next(err);
res.cookie('session', session_id);
res.send(200);
});
}
});
}
this.validateLogin = function(data, callback) {
"use strict";
// Callback to pass to MongoDB that validates a user document
function validateUserDoc(err, user) {
"use strict";
if (err) return callback(err, null);
if (user) {
if (bcrypt.compareSync(data.password, user.password)) {
callback(null, user);
} else {
callback(new Error("Invalid password/email"), null);
}
}else{
// no such user error
callback(new Error("no such user"), null);
}
}
users.findOne({ '_id' : data.email }, validateUserDoc);
process.nextTick(function() {
callback(null, data);
});
}
答案 0 :(得分:0)
看起来传递给validateLogin
的回调被调用两次:
validateUserDoc
; process.nextTick
回调(该部分似乎是一些遗留代码);