【发布时间】:2019-11-15 01:58:50
【问题描述】:
我已经创建了这个并一直在使用这个模型。但是现在我添加了@pre<Offer>("save",因为我想在保存之前对数据进行一些检查并相应地调整值。
我的问题是,当我首先进行更新时,控制台日志永远不会得到输出。如果我发布isOfferLive:true,那么我永远无法设置this.isOfferLive = false;,因此永远不会调用pre
我想念你什么?
我正在使用
"reflect-metadata": "^0.1.13",
"typegoose": "^5.7.2",
"mongoose": "^5.6.3",
emitDecoratorMetadata and experimentalDecorators are be enabled in tsconfig.json
我注意到的一件事是,在查看 consoe 日志时,Model.create 上会调用 hooks 而不是 Model.updateOne。
我如何让它们也能在更新上工作
我的班级
@pre<Offer>("save", function(next: HookNextFunction) {
console.log("[**** save]: " + this.isOfferLive);
this.isOfferLive = false;
if (!isOfferValid(this)) {
this.isOfferLive = false;
}
next();
})
export class Offer extends Typegoose {
@prop({ required: true, default: false })
public isOfferLive: boolean;
}
export const Model = new Offer().getModelForClass(Offer);
【问题讨论】:
标签: mongodb typescript mongoose typegoose