在 mongodb 中使用 bcrypt 更新用户密码时出错
error when updating user password with bcrypt in mongodb
const {password, value} = body.password;
// compares the old and new password
const passwordMatch = await bcrypt.compare(password, student.password);
if(!passwordMatch) return res.status(400).json({message:"passwords do not match"})
//hashes the password
const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(value, salt);
student.password = hashedPassword;
正在使用“save()”保存文档
下面是我得到的错误
"error": {
"errors": {
"password": {
"stringValue": "\"{ password: 'abcde', value: '123456' }\"",
"kind": "string",
"value": {
"password": "abcde",
"value": "123456"
},
"path": "password",
"reason": null
}
},
"_message": "Secondary validation failed",
"message": "Secondary validation failed: password: Cast to string failed for value \"{ password: 'abcde', value: '123456' }\" at path \"password\""
}
有什么帮助吗?我正在使用节点、表达和 mongodb。我也在使用邮递员进行测试。我不知道是什么导致了这个问题。
下面是输入的结构(即req.body)
{
"password": {
"password": "abcde",
"value": "123456"
}
}
哪里
- password = 旧密码(即数据库中存在的密码)
- value = 要更改为的新密码
假设您从某处获得 students._id
router.patch("/", async (req, res) => {
try {
const { password, newPassword } = req.body;
const validPass = await bcrypt.compare(password, student.password);
if (!validPass) return res.status(400).json("Invalid Password");
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(newPassword, salt);
await Student.findOneAndUpdate(
{ _id: student._id },
{ $set: { password: hashedPassword } },{ useFindAndModify: false }
);
res.status(200).send("Password Updated");
} catch (err) {
res.status(400).send({ message: err });
}
});
你会看到这样的东西,如果这有帮助请告诉我
const {password, value} = body.password;
// compares the old and new password
const passwordMatch = await bcrypt.compare(password, student.password);
if(!passwordMatch) return res.status(400).json({message:"passwords do not match"})
//hashes the password
const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(value, salt);
student.password = hashedPassword;
正在使用“save()”保存文档
下面是我得到的错误
"error": {
"errors": {
"password": {
"stringValue": "\"{ password: 'abcde', value: '123456' }\"",
"kind": "string",
"value": {
"password": "abcde",
"value": "123456"
},
"path": "password",
"reason": null
}
},
"_message": "Secondary validation failed",
"message": "Secondary validation failed: password: Cast to string failed for value \"{ password: 'abcde', value: '123456' }\" at path \"password\""
}
有什么帮助吗?我正在使用节点、表达和 mongodb。我也在使用邮递员进行测试。我不知道是什么导致了这个问题。
下面是输入的结构(即req.body)
{
"password": {
"password": "abcde",
"value": "123456"
}
}
哪里
- password = 旧密码(即数据库中存在的密码)
- value = 要更改为的新密码
假设您从某处获得 students._id
router.patch("/", async (req, res) => {
try {
const { password, newPassword } = req.body;
const validPass = await bcrypt.compare(password, student.password);
if (!validPass) return res.status(400).json("Invalid Password");
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(newPassword, salt);
await Student.findOneAndUpdate(
{ _id: student._id },
{ $set: { password: hashedPassword } },{ useFindAndModify: false }
);
res.status(200).send("Password Updated");
} catch (err) {
res.status(400).send({ message: err });
}
});
你会看到这样的东西,如果这有帮助请告诉我