[英]How to Fix Express Routing
router.post('/login', (req, res, next) => {
User.findOne({ "email": req.body.email }, (err, user) => {
if (err) throw err;
if (!user){
res.json({
success: false,
message: 'Authenticated failed, User not found'
});
} else if (user) {
var validPassword = user.comparePassword(req.body.password);
if (!validPassword) {
res.json({
success: false,
message: 'Authentication failed. Wrong Password'
});
} else {
var token = jwt.sign({
user: user
}, config.secret, {
expiresIn: '7d'
});
res.json({
success: true,
message: "enjoy your token",
token: token
});
}
}
});
錯誤:
events.js:180 拋出錯誤; // 未處理的“錯誤”事件 ^ 錯誤 [ERR_UNHANDLED_ERROR]:未處理的錯誤。 (Incorrect arguments) at Function.emit (events.js:178:17) at C:\Users\farid\Desktop\Ecomerce\server\node_modules\mongoose\lib\model.js:4640:13 at C:\Users\ farid\Desktop\Ecomerce\server\node_modules\mongoose\lib\query.js:4345:12 at process.nextTick (C:\Users\farid\Desktop\Ecomerce\server\node_modules\mongoose\lib\query.js:2841 :28) 在 process._tickCallback (internal/process/next_tick.js:61:11)
我不知道我錯過了什么,有人可以突出代碼中的錯誤嗎?
在您的代碼中使用 try catch 塊,希望這會對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.