【发布时间】:2021-11-30 02:23:11
【问题描述】:
第一次命中请求成功,但第二次失败。我的代码有问题吗?谢谢
我的错误:
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);
}
})
【问题讨论】: