When I uncomment one line of code, I receive this error: _http_outgoing.js:359 throw new Error('Can\'t set headers after they are sent.');
When I uncomment one line of code, I receive this error: _http_outgoing.js:359 throw new Error('Can\'t set headers after they are sent.');
当我在下面的代码中取消注释这一行:return done(null, false, { message: 'Incorrect username' });
时,Node.js运行没有任何错误,否则,Node.js会出现后面提到的错误:
//we need to define a local Strategy to authenticate with username and password
passport.use(new LocalStrategy(
function(username, password, done) { //start of custom code
//from here starts our custom code (inside this function scope)
//outside this scope is from passport API documentation
//process.nextTick(function() {
UserFind(username, function(err, user) {
if (err) {
return done(err);
} else if (!user || user.length == 0) {
//When I uncomment the following line of code, an error occurs, but when I comment out the following, no error is received:
//return done(null, false, { message: 'Incorrect username' });
} else {
console.log('user._id:' + user._id);
console.log('typeof user: ' + typeof(user));
console.log('user.username: ' + user.username);
console.log('user.password: ' + user.password);
if (password !== user.password) {
return done(null, false, { message: 'Incorrect password' });
}
return done(null, user);
}
})
//}) //process.nextTick
} //end of custom code
));
当我取消注释上述行时收到的错误:
_http_outgoing.js:359
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:359:11)
at ServerResponse.header (/home/ict/Documents/hookahDB/serverjs/node_modules/express/lib/response.js:719:10)
at ServerResponse.location (/home/ict/Documents/hookahDB/serverjs/node_modules/express/lib/response.js:836:15)
at ServerResponse.redirect (/home/ict/Documents/hookahDB/serverjs/node_modules/express/lib/response.js:874:18)
at allFailed (/home/ict/Documents/hookahDB/serverjs/node_modules/passport/lib/middleware/authenticate.js:132:20)
at attempt (/home/ict/Documents/hookahDB/serverjs/node_modules/passport/lib/middleware/authenticate.js:167:28)
at Strategy.strategy.fail (/home/ict/Documents/hookahDB/serverjs/node_modules/passport/lib/middleware/authenticate.js:284:9)
at verified (/home/ict/Documents/hookahDB/serverjs/node_modules/passport-local/lib/strategy.js:82:30)
at /home/ict/Documents/hookahDB/serverjs/index.js:158:28
at /home/ict/Documents/hookahDB/serverjs/index.js:144:16
我想知道为什么那一行是错误的原因以及我该如何解决它。谢谢
编辑
回调函数如下所示:
function UserFind(username, cb) {
db.view('users/by_username', function(err, res) {
if (err) {
//db.view returned error
return cb(err);
}
res.forEach(function(key, value, id) {
//1st input=key|username, 2nd input=value|userDocument, 3rd input=id|_id
//console.log('key: '+key+' row: '+row+' id: '+ id);
if (username === key) {
//found the user
return cb(false, value);
}
})
//couldn't find the user by forEach loop
return cb(false, false);
})
}
该错误消息是由处理异步响应时的计时错误引起的,该错误导致您在响应已发送后尝试发送响应数据。
当人们将快速路由中的异步响应视为同步响应并且他们最终发送了两次数据时,通常会发生这种情况。
您应该在声明中添加其他内容
if (password !== user.password) {
return done(null, false, { message: 'Incorrect password' });
} else {
return done(null, user);
}
更新:
function UserFind(username, cb) {
var userFound = false, userVal = "";
db.view('users/by_username', function(err, res) {
if (err) {
//db.view returned error
return cb(err);
}
res.forEach(function(key, value, id) {
//1st input=key|username, 2nd input=value|userDocument, 3rd input=id|_id
//console.log('key: '+key+' row: '+row+' id: '+ id);
if (username === key) {
//found the user
userFound = true;
userVal = value;
}
});
if (userFound) {
return cb(false, userVal);
} else {
// User did not found
return cb(false, false);
}
})
}
当我在下面的代码中取消注释这一行:return done(null, false, { message: 'Incorrect username' });
时,Node.js运行没有任何错误,否则,Node.js会出现后面提到的错误:
//we need to define a local Strategy to authenticate with username and password
passport.use(new LocalStrategy(
function(username, password, done) { //start of custom code
//from here starts our custom code (inside this function scope)
//outside this scope is from passport API documentation
//process.nextTick(function() {
UserFind(username, function(err, user) {
if (err) {
return done(err);
} else if (!user || user.length == 0) {
//When I uncomment the following line of code, an error occurs, but when I comment out the following, no error is received:
//return done(null, false, { message: 'Incorrect username' });
} else {
console.log('user._id:' + user._id);
console.log('typeof user: ' + typeof(user));
console.log('user.username: ' + user.username);
console.log('user.password: ' + user.password);
if (password !== user.password) {
return done(null, false, { message: 'Incorrect password' });
}
return done(null, user);
}
})
//}) //process.nextTick
} //end of custom code
));
当我取消注释上述行时收到的错误:
_http_outgoing.js:359 throw new Error('Can\'t set headers after they are sent.'); ^
Error: Can't set headers after they are sent. at ServerResponse.setHeader (_http_outgoing.js:359:11) at ServerResponse.header (/home/ict/Documents/hookahDB/serverjs/node_modules/express/lib/response.js:719:10) at ServerResponse.location (/home/ict/Documents/hookahDB/serverjs/node_modules/express/lib/response.js:836:15) at ServerResponse.redirect (/home/ict/Documents/hookahDB/serverjs/node_modules/express/lib/response.js:874:18) at allFailed (/home/ict/Documents/hookahDB/serverjs/node_modules/passport/lib/middleware/authenticate.js:132:20) at attempt (/home/ict/Documents/hookahDB/serverjs/node_modules/passport/lib/middleware/authenticate.js:167:28) at Strategy.strategy.fail (/home/ict/Documents/hookahDB/serverjs/node_modules/passport/lib/middleware/authenticate.js:284:9) at verified (/home/ict/Documents/hookahDB/serverjs/node_modules/passport-local/lib/strategy.js:82:30) at /home/ict/Documents/hookahDB/serverjs/index.js:158:28 at /home/ict/Documents/hookahDB/serverjs/index.js:144:16
我想知道为什么那一行是错误的原因以及我该如何解决它。谢谢
编辑
回调函数如下所示:
function UserFind(username, cb) {
db.view('users/by_username', function(err, res) {
if (err) {
//db.view returned error
return cb(err);
}
res.forEach(function(key, value, id) {
//1st input=key|username, 2nd input=value|userDocument, 3rd input=id|_id
//console.log('key: '+key+' row: '+row+' id: '+ id);
if (username === key) {
//found the user
return cb(false, value);
}
})
//couldn't find the user by forEach loop
return cb(false, false);
})
}
该错误消息是由处理异步响应时的计时错误引起的,该错误导致您在响应已发送后尝试发送响应数据。
当人们将快速路由中的异步响应视为同步响应并且他们最终发送了两次数据时,通常会发生这种情况。
您应该在声明中添加其他内容
if (password !== user.password) {
return done(null, false, { message: 'Incorrect password' });
} else {
return done(null, user);
}
更新:
function UserFind(username, cb) {
var userFound = false, userVal = "";
db.view('users/by_username', function(err, res) {
if (err) {
//db.view returned error
return cb(err);
}
res.forEach(function(key, value, id) {
//1st input=key|username, 2nd input=value|userDocument, 3rd input=id|_id
//console.log('key: '+key+' row: '+row+' id: '+ id);
if (username === key) {
//found the user
userFound = true;
userVal = value;
}
});
if (userFound) {
return cb(false, userVal);
} else {
// User did not found
return cb(false, false);
}
})
}