![](/img/trans.png)
[英]ExpressJS - Unhandled rejection Error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[英]ExpressJS Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
第一次命中请求成功,第二次失败。 我的代码有问题吗? 谢谢你
我的错误:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
我的代码 =
router.post("/login", async (req, res) => {
try {
const user = await User.findOne({ username: req.body.username });
!user && res.status(401).json("Wrong credentials!");
const hashedPassword = CryptoJS.AES.decrypt(
user.password,
process.env.PASS_SEC
);
const OriginalPassword = hashedPassword.toString(CryptoJS.enc.Utf8);
OriginalPassword !== req.body.password &&
res.status(401).json("Wrong credentials!");
const accessToken = jwt.sign(
{
id: user._id,
isAdmin: user.isAdmin,
},
process.env.JWT_SEC,
{ expiresIn: "3d" }
);
const { password, ...others } = user._doc;
res.status(200).json({...others, accessToken});
} catch (err) {
res.status(500).json(err);
}
})
首先将值传播到您的 object 然后更新值
const {...others, password } = user._doc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.