简体   繁体   English

如何在mongodb中更新密码

[英]How update the password in mongodb

 app.get('/changePass', function(req, res) {
password = req.body.password;
password1 = req.body.password1;
xyz = req.body.xyz;

User.find({ email: xyz, password: password }, function(err, data) {
    if (err) {
        throw err;
    }
    if (data.length === 0) {
        return res.json({ success: false, data: 'please enter valid 
   Old password' })
    } else {

        // User.update({ password: password }, {
        //     $set: {
        //         password: password1
        //     }
        // });
         var usr = new User();
        usr.update({ "password":password }, password1, { upsert:true 
   });
        //usr.password = password;
        usr.save(function(err, data) {
            if (err)
                throw err;
            return res.json({ success: true, data: 'password changed 
 successfully' });
        })
    }
   })

how i update password in mongodb i am matching password to password and updating password to password1.this is giving me alert with please enter valid old password. 我如何在mongodb中更新密码,我将密码与密码匹配并将密码与password1.this匹配,请输入有效的旧密码,以提醒我。 xyz is cookie that contains email. xyz是包含电子邮件的cookie。

You should use findOne on model to return userObject. 您应该在模型上使用findOne返回userObject。 Then you can update the object that was found like regular javascript object and save it with .save function. 然后,您可以更新找到的对象(如常规javascript对象)并使用.save函数保存。

User.findOne({email: xyz, password: req.body.password1}, function(err, user){
 if(err)return handleErr(err);
 user.password = req.body.password2;
 user.save(function(err){
    if(err)return handleErr(err);
    //user has been updated
  });
});

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

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