【发布时间】:2019-02-20 01:47:19
【问题描述】:
每当我包含这个 pre() 函数时,邮递员都会返回错误, 它返回一个错误,否则它的工作和所有内容都使用 mongodb 存储在 db 中。 我使用的 ES6 格式或其他格式有问题吗?
下面是代码:
// usersschema 是模式的名称 // // SALT_I = 10 //
userSchema.pre('save', next => {
if (this.isModified('password')) {
bcrypt.genSalt(SALT_I, (err, salt) => {
if (err)
return next(err)
bcrypt.hash(this.password, salt, (err, hash) => {
if (err)
return next(err)
this.password = hash
next()
})
})
} else
next()
})
这是邮递员的错误:
{
"success": false,
"err": {}
}
就像我正在使用该功能发出帖子请求一样:
app.post('/api/users/register', (req, res) => {
const user = new User(req.body)
user.save((err, data) => {
if (err) return res.json({ success: false, err })
res.status(200).json({
success: true,
userdata: data
})
})
})
【问题讨论】:
-
“邮递员返回错误” - 为什么要对错误消息保密?
-
我已经编辑了!
标签: javascript mongodb ecmascript-6 postman mongoose-schema