查找附加的我的代码试图以用户身份登录,它给了我http承诺标头错误(节点:17772) UnhandledPromiseRejectionWarning: error ERR_HTTP_HEADERS_SENT:无法在将标题发送给ServerResponse.header ServerResponse.setHeader (_http_outgoing.js:561:11)的客户端后设置
在(C:\Users\DELL\Desktop\codes\REACT-BLOG\blogbackend\node_modules\express\lib\response.js:170:12) ServerResponse.send
在(C:\Users\DELL\Desktop\codes\REACT-BLOG\blogbackend\node_modules\express\lib\response.js:267:15) ServerResponse.json
在C:\Users\DELL\Desktop\codes\REACT-BLOG\blogbackend\routes\auth.js:39:25 at processTicksAndRejections (内部/进程/任务队列),95:95:5(节点:17772),UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误起源于在异步函数中抛出而不带catch块,或者拒绝使用.catch()处理的承诺。若要在未处理的允诺拒绝时终止节点进程,请使用CLI标志--unhandled-rejections=strict (请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。(拒绝证: 2)。
下面是我的密码。
router.post ("/login", async (req, res) => {
try{
const user = await User.findOne({ username: req.body.username });
!user && res.status(400).json("wrong credentials!");
const validated = await bcrypt.compare(req.body.password, user.password);
!validated && res.status(400).json("wrong credentials!");
const { password, ...others } = user._doc;
res.status(200).json(others);
} catch (err) {
res.status(500).json(err);
}
});发布于 2022-07-23 04:48:57
router.post ("/login", async (req, res) => {
try{
const user = await User.findOne({ username: req.body.username });
return !user && res.status(400).json("wrong credentials!");
const validated = await bcrypt.compare(req.body.password, user.password);
return !validated && res.status(400).json("wrong credentials!");
const { password, ...others } = user._doc;
res.status(200).json(others);
} catch (err) {
res.status(500).json(err);
}});
https://stackoverflow.com/questions/73084619
复制相似问题