0
我想从我的预保存钩子中调用一个函数,该钩子更新正在保存/更新的实例中的属性。我不想在schema方法中再次调用save()。另外,下面只是一个例子,但我有一些shema方法变得很长,我不想将它们包含在pre save钩子中。从预保存钩子和更新属性中调用模式方法
UserSchema.pre('save', function(next) {
const user = this;
...
if (user.isModified('something')) {
user.checkSomething();
}
...
next();
});
UserSchema.method.checkSomething() = function() {
const user = this;
if(user.something > 5) {
// Any way to update the below property without calling save() again?
user.somethingElse = true;
}
}
看来这个改变并不是永久性的,一旦函数返回。
谢谢。