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;
}
}
看來這個改變並不是永久性的,一旦函數返回。
謝謝。