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 this line: return done(null, false, { message: 'Incorrect username' }); in the following code, Node.js runs without any error, otherwise, Node.js gives an error me
Solution 1:
That error message is caused by a timing error in the handling of an async response that causes you to attempt to send data on a response after the response has already been sent.
It usually happens when people treat an async response inside an express route as a synchronous response and they end up sending data twice.
You should add else in your statement
if (password !== user.password) {
return done(null, false, { message: 'Incorrect password' });
} else {
return done(null, user);
}
Update:
functionUserFind(username, cb) {
var userFound = false, userVal = "";
db.view('users/by_username', function(err, res) {
if (err) {
//db.view returned errorreturn 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);
}
})
}
Post a Comment for "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.');"