【发布时间】:2022-01-09 17:54:26
【问题描述】:
我试图在我的 userController.js 中更新UserPassword 并遇到以下错误。不确定这可能是什么错误。任何意见和知识将不胜感激。
在邮递员上收到此错误
{
"msg": "Cannot read properties of null (reading 'comparePassword')"
}
身体
{
"oldPassword":"secret",
"newPassword":"newsecret"
}
userController.js
const updateUserPassword = async (req, res) => {
const { oldPassword, newPassword } = req.body;
if (!oldPassword || !newPassword) {
throw new CustomError.BadRequestError('Please provide both values');
}
const user = await User.findOne({ _id: req.user.userId });
const isPasswordValid = await user.comparePassword(oldPassword);
if (!isPasswordValid) {
throw new CustomError.UnauthenticatedError('Invalid Credentials');
}
user.password = newPassword;
await user.save();
res.status(StatusCodes.OK).json({ msg: 'Success! Password Updated.' });
};
【问题讨论】: