Mongoose返回承诺预保存功能

时间:2017-06-14 14:05:28

标签: mongoose es6-promise mongoose-schema

我想问你这个代码:

UserSchema.pre('save', function userPreSave(next) {
    const user = this;
    if (this.isModified('password') || this.isNew) {
        return bcrypt.hash(user.password, 10)
            .then((hash) => {
                user.password = hash;
                return next();
            })
            .catch(err => next(err));
    }
    return next();
});

您能在此代码中看到任何问题或阻止内容吗?特别是关于这一部分:return bcrypt.hash可以从中回复承诺吗?不会有任何副作用吗?

0 个答案:

没有答案