我使用 Node JS、Express 和 Mongo DB 开发了 REST 服务。 我已经定义了一个 POST 请求来将用户添加到数据库表中,当我尝试(在本地主机上)POSTMAN 的 REST 服务时,他会工作并将用户添加到表中,但 Node 应用程序崩溃(server.js),我收到此消息错误: 抛出新的 ERR_HTTP_HEADERS_SENT('set'); ^
错误 [ERR_HTTP_HEADERS_SENT]:将 header 发送到客户端后无法设置 header 该错误引用了我的代码:
router.post('/user', VerifyToken, function (req, res) {
User.findOne({ username: req.body.username }, function (err, user) {
if (user) return res.status(400).send({status: 'ko', error: { msg: 'The username you have entered is already associated with another user.'}});
var hashedPassword = bcrypt.hashSync(req.body.password, 8);
User.create({
firstname:req.body.firstname,
surname:req.body.surname,
username:req.body.username,
email: req.body.email,
password: hashedPassword,
farmId: req.body.farmId,
roles: req.body.roles,
isVerified : req.body.isVerified,
statusUser : req.body.statusUser
},
function (err, user) {
if (err) return res.status(500).send("There was a problem adding the user to the database.");
res.status(200).send({status: 'ok', data: { msg: 'User saved', user: user}});
var client = nodemailer.createTransport(sgTransport(options));
var email = {
from: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb8584998e9b8792c69f87889b879e98ab9f998e87878e8984998cc5888486" rel="noreferrer noopener nofollow">[email protected]</a>',
to: req.body.email,
subject: 'Registration successfully confirmed',
text: 'Hi '+ req.body.username + ',\n\nyour account has been registered.\n\nAre you the farm owner?' +
'\n\nPlease go to this link [CMS link] to create your Profile and start to use the App Plus!\n\n'+
'If you are a simple user, please just use your credentials to login to the App Plus section into the new App!\n\n'+
'Download for free from App Store or Google Play!\n\nRegards,\n\nTrelleborg TLC Plus team'
};
client.sendMail(email, function(err, json){
if (err){
return res.status(500).send({ msg: err.message });
}
else {
res.status(200).send({status: 'ok', data: { msg: 'A verification email has been sent to ' + user.email + '.'}} )
}
});
});
});
});
该行错误位于发送验证邮件的 res.status(200) 上。 为什么我的应用程序崩溃了,错误在哪里? 请问有什么帮助吗? 谢谢
最佳答案
发生这种情况是因为您发送了两次响应。因此,错误:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
将代码更改为以下内容:
router.post('/user', VerifyToken, function (req, res) {
User.findOne({ username: req.body.username }, function (err, user) {
if (user) return res.status(400).send({status: 'ko', error: { msg: 'The username you have entered is already associated with another user.'}});
var hashedPassword = bcrypt.hashSync(req.body.password, 8);
User.create({
firstname:req.body.firstname,
surname:req.body.surname,
username:req.body.username,
email: req.body.email,
password: hashedPassword,
farmId: req.body.farmId,
roles: req.body.roles,
isVerified : req.body.isVerified,
statusUser : req.body.statusUser
},
function (err, user) {
if (err) return res.status(500).send("There was a problem adding the user to the database.");
var client = nodemailer.createTransport(sgTransport(options));
var email = {
from: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f997968b9c899580d48d959a89958c8ab98d8b9c95959c9b968b9ed79a9694" rel="noreferrer noopener nofollow">[email protected]</a>',
to: req.body.email,
subject: 'Registration successfully confirmed',
text: 'Hi '+ req.body.username + ',\n\nyour account has been registered.\n\nAre you the farm owner?' +
'\n\nPlease go to this link [CMS link] to create your Profile and start to use the App Plus!\n\n'+
'If you are a simple user, please just use your credentials to login to the App Plus section into the new App!\n\n'+
'Download for free from App Store or Google Play!\n\nRegards,\n\nTrelleborg TLC Plus team'
};
client.sendMail(email, function(err, json){
if (err){
return res.status(500).send({ msg: err.message });
}
res.status(200).send({status: 'ok', data: { msg: 'A verification email has been sent to ' + user.email + '.'}, message: 'User saved.'} )
});
});
});
});
关于node.js - Node js POST请求错误错误[ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580102/