简体   繁体   中英

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; // Unhandled 'error' event ^ Error [ERR_UNHANDLED_ERROR]: 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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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