Я проверяю адрес электронной почты с помощью ссылки для подтверждения. Но у меня возникла ошибка при обратном вызове. Зачем?Unhandled rejection Ошибка: не удается установить заголовки после их отправки
Error : Unhandled rejection Error: Can't set headers after they are sent.
if (req.body.userId && req.body.verificationCode) {
db.User.findOne({
where: {
ID: req.body.userId
}
}).then(function(result) {
if (!result.EMAIL_IS_VERIFIED) {
return db.EmailVerify.findOne({
where: {
VERIFICATION_CODE: req.body.verificationCode
}
});
} else {
console.log("This email is already verified")
cb({
message: 'This email is already verified'
}, null);
}
}).then(function(result) {
if (result) {
var now = Date.now();
if (Math.round(Math.abs((new Date(now) - new Date(result.CREATED_AT))/(24 * 60 * 60 * 1000))) < 1)
return db.User.update({
EMAIL_IS_VERIFIED: true,
STATUS: 'active'
}, {
where: {
ID: req.body.userId,
EMAIL: result.EMAIL
}
});
else {
console.log("Verification Link is experied")
cb({
message: 'Your verification link is expired'
}, null);
}
} else {
console.log("Sorry, Your verification link is not working.")
cb({
message: "Sorry, Your verification link is not working."
}, null);
}
}).then(function() {
return db.User.findOne({
where: {
id: req.body.userId
}
});
}).then(function(theUser) {
return security.makeToken(theUser);
}).then(function(token) {
cb(null, {
message: messages.user.signUpSuccess,
token: token
});
}).catch(function(err) {
var em = err.message.indexOf('Validation') >= 0 ? "Your provided email is associated with another account!" : err.message;
cb({
message: em,
code: err.code
}, null);
})
} else {
cb({
message: messages.user.missingInfoAtVerification
}, null);
}