【发布时间】:2021-03-28 17:57:01
【问题描述】:
我正在使用 Mongoose 驱动程序在 NodeJS 中创建一个 REST API。我想在保存密码之前对密码进行哈希处理。同样,我使用的是 Mongoose Schema,我在其中为我的用户模型创建了一个 userSchema。对于散列,我使用了以下函数。
userSchema.pre('save', async (next) => {
const user = this;
console.log(user);
console.log(user.isModified);
console.log(user.isModified());
console.log(user.isModified('password'));
if (!user.isModified('password')) return next();
console.log('just before saving...');
user.password = await bcrypt.hash(user.password, 8);
console.log('just before saving...');
next();
});
但在创建用户或修改用户时,我收到错误 500,并且返回 {}。我的路由器如下。
router.post('/users', async (req, res) => {
const user = User(req.body);
try {
await user.save();
res.status(201).send(user);
} catch (e) {
res.status(400).send(e);
}
});
router.patch('/users/:id', async (req, res) => {
const updateProperties = Object.keys(req.body);
const allowedUpdateProperties = [
'name', 'age', 'email', 'password'
];
const isValid = updateProperties.every((property) => allowedUpdateProperties.includes(property));
if (!isValid) {
return res.status(400).send({error: "Invalid properties to update."})
}
const _id = req.params.id;
try {
const user = await User.findById(req.params.id);
updateProperties.forEach((property) => user[property] = req.body[property]);
await user.save();
if (!user) {
return res.status(404).send();
}
res.status(200).send(user);
} catch (e) {
res.status(400).send(e);
}
});
以下是我的控制台输出。
Server running on port 3000
{}
undefined
注释掉 userSchema.pre('save', ...) 一切都按预期工作。请你能帮我弄清楚我哪里出错了。
【问题讨论】: