使用明上文将标题发送给客户端后无法设置标题
const emailaddress = req.body.emailaddress;
const password = req.body.password;
if (emailaddress && password) {
const query = `SELECT * FROM users where email=? AND password=?`;
connection.query(
query,
[emailaddress, password],
function (err, results, fields) {
if (err) {
res.send({
code: 500,
failed: "Error ocurred",
});
}
if (results.length > 0) {
req.session.regenerate(function (err) {
// if (err) next(err);
req.session.loggedin = true;
req.session.emailaddress = req.body.emailaddress;
req.session.save(function (err) {
// if (err) return next(err);
res.redirect("/home");
});
});
} else {
res.send("Incorrect email address and/or password!");
}
res.end();
}
);
} else {
res.send("Please enter username and/or password");
res.end();
}
我尝试使用上面的代码使用快递会话来存储会话值。但是它给出以下错误。
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
我怎么避免这种情况。我已经尝试了许多解决方案。我正在遵循他们的文档。请帮忙。谢谢。 “ https://www.npmjs.com/package/package/express-session”
const emailaddress = req.body.emailaddress;
const password = req.body.password;
if (emailaddress && password) {
const query = `SELECT * FROM users where email=? AND password=?`;
connection.query(
query,
[emailaddress, password],
function (err, results, fields) {
if (err) {
res.send({
code: 500,
failed: "Error ocurred",
});
}
if (results.length > 0) {
req.session.regenerate(function (err) {
// if (err) next(err);
req.session.loggedin = true;
req.session.emailaddress = req.body.emailaddress;
req.session.save(function (err) {
// if (err) return next(err);
res.redirect("/home");
});
});
} else {
res.send("Incorrect email address and/or password!");
}
res.end();
}
);
} else {
res.send("Please enter username and/or password");
res.end();
}
I tried using the above code using express-session to store session values. but it gives the following error.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
How can I avoid this. I already tried many solutions. I am following their documentation. Please help. Thank you.
https://www.npmjs.com/package/express-session
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能的问题:
else block呼叫<代码>发送和在外面的最后一行,否则调用
end()
。因此,如果它属于其他块,
send
和end
将被称为引起该错误。您还可以多次呼叫
next(err)
,而不知道这是下一个处理程序,我可以肯定地告诉您,但他们也可能正在调用send
。这将属于与上述相同的情况。Possible problems:
Else block calls
send
and last line outside if and else callsend()
.So if it falls into the Else block, both
send
andend
will be called causing that error.You also have multiple call to
next(err)
, without knowing that is the next handler, I can tell you for sure, but they might be callingsend
as well. Which would fall into the same scenario as above.我看到您回答后的代码进行了一些更改。
让我重试,您不能使用
res.send
和res.end
一起选择一个。另外,您需要确保您仅调用res.send
一次。您的代码应该是这样的:
I see you made some changes in your code after my answer.
Let me try again, you cannot use
res.send
andres.end
together, you need to pick one. Also you need to make sure you are only callingres.send
once.your code should be something like this: