简体   繁体   English

NodeJS和Mongodb findOneAndUpdate

[英]NodeJS and Mongodb findOneAndUpdate

I need help to figure this out. 我需要帮助来解决这个问题。 The update function is called to reset a password on database. 调用update函数以重置数据库密码。 I got this error. 我得到了这个错误。

TypeError: Cannot read property 'password' of undefined TypeError:无法读取未定义的属性“密码”

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 谢谢

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

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