简体   繁体   English

如何修复快速路由

[英]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
            });
        }
    }

});

Error:错误:

events.js:180 throw err; events.js:180 抛出错误; // Unhandled 'error' event ^ Error [ERR_UNHANDLED_ERROR]: Unhandled error. // 未处理的“错误”事件 ^ 错误 [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) at process._tickCallback (internal/process/next_tick.js:61:11) (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)

I don't know what I am missing, can someone highlight the error in the code?我不知道我错过了什么,有人可以突出代码中的错误吗?

Use try catch block in your code hope this will help you.在您的代码中使用 try catch 块,希望这会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM