简体   繁体   中英

NodeJS and Mongodb findOneAndUpdate

I need help to figure this out. The update function is called to reset a password on database. I got this error.

TypeError: Cannot read property 'password' of undefined

module.exports.update = function (token, req, res) {
    User.findOneAndUpdate({resetPasswordToken: token, password: req.body.password, resetPasswordExpires: {$gt: Date.now()}}, function(err) {
        if (err) throw err;
        return res.sendStatus(200);
        console.log(User);
    });
}

router.post('/forgot', function (req, res) {

    var password = req.body.passwordnew;
    var password2 = req.body.passwordnew2;

        var update = User.update(password, password2, function (err, user) {
            //userToken = token;
            if (!update) {
                console.log("token2 = " + req.params.resetPasswordToken);
                req.flash('error', 'Password reset token is invalid or has expired.');
                return res.redirect('forgot');
            }
            else {
                user.save(function (err) {
                    user.password = password;
                    user.password2 = password2;
                    user.resetPasswordToken = undefined;
                    user.resetPasswordExpires = undefined;
                });
                console.log("save new password");
            }
        });
    //}

})

when you use update method of mongoose 1st parameter will be query ( by which you can find that doccument in collection) and 2nd will be what you want to update ..

So what query you make is not make sense , it will be like :

var userId=user; //mongoId
var newPassWord=req.body.passwordnew;

 User.update({_id:userId}, {password:newPassWord}, callbackFunction);

this will update password of that perticular user.

Thanks

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