i'm trying to change the password of username using local mongoose
i tried to use setPassword function but it does not seem to work
router.put('/admin/users/:username', function(req,res){
User.findByUsername.then(function(sanitizedUser){
if (sanitizedUser){
sanitizedUser.setPassword(req.body.password, function(){
sanitizedUser.save();
res.redirect("back");
});
} else {
res.redirect("back");
}
},function(err){
console.error(err);
})
});
除了setpassword之外,还有其他解决方案吗? 我到底做错了什么?
答案 0 :(得分:0)
我已经发布了我倾向于重设密码的操作(部分密码包括对密码进行哈希处理,但其余部分相同)。除了我正在使用async/await
const { password } = req.body;
let restP = new User();
try {
const newP = await restP.generateHash(password.password);
const resetP = await User.findByIdAndUpdate(
req.params.id,
{ $set: { passwordHash: newP } },
{
fields: { passwordHash: 0 },
new: true
}
)
// return image user object
res.send(resetP);
} catch (error) {
console.log(error);
return res.status(400).send(error);
}