node.js - 在 findOneAndUpdate 中生成散列密码
问题描述
这是我对 findOneAndUpdate 的查询
const { email, password, id } = req.body
Artist.findOneAndUpdate({ _id: id }, { $set: req.body }).then((artist) => {
return res.json({
success: true,
message: "Invitation sent."
});
})
这是我的架构
var artistSchema = new mongoose.Schema({
name: { type: String, default: '' },
password: { type: String, default: '' }
})
artistSchema.pre('findOneAndUpdate', function (next) {
console.log('------------->>>>>> findOneAndUpdate: ');
console.log(this.password) // why undefined?
next();
});
我想在用户更新详细信息时创建一个散列密码
解决方案
const { email, password, id } = req.body;
Artist.findByIdAndUpdate(id, { $set: req.body }).then(artist => {
return res.json({
success: true,
message: "Invitation sent."
});
});
bcrypt 示例
var artistSchema = new mongoose.Schema({
name: { type: String, default: "" },
password: { type: String, default: "" }
});
artistSchema.pre("update", function(next) {
bcrypt.hash(this.password, 10, function(err, hash) {
if (err) return next(err);
this.password = hash;
next();
});
});
推荐阅读
- javascript - 这个特定的嵌套 for 循环如何在原生 JavaScript 中工作?
- python - 熊猫选择相应 nan 值的前一列值
- python - jinja2.exceptions.TemplateNotFound 错误的问题
- c++ - 编译器差异:在 const 成员函数中定义的可变 lambda 中捕获值时的“this”类型
- bash - 使用变量设置 Docker GitLab root 密码?
- java - equalsIgnoreCase 在自身上被调用。错误
- reactjs - Material-UI:`css` 功能已弃用。改用 `styleFunctionSx`
- python - 用于裁剪的 GUI 测量图片大小
- reactjs - “TypeError:graphql.isType 不是函数”
- reactjs - 收到来自 firebase 云消息传递的消息后调度操作