当仅使用Typegoose进行保存操作时,挂钩在updateOne上不起作用

时间:2019-07-04 10:31:41

标签: mongodb typescript mongoose typegoose

我已经创建了这个模型,并且一直在使用这个模型。但是现在我添加了@pre<Offer>("save",因为我想在保存并相应调整值之前对数据进行一些检查。

我的问题是,当我第一次进行更新时,控制台日志永远不会输出。然后,如果我发布this.isOfferLive = false;,就永远无法设置isOfferLive:true,因此永远不会调用pre

我想念你什么?

我正在使用

    "reflect-metadata": "^0.1.13",
    "typegoose": "^5.7.2",
    "mongoose": "^5.6.3",
emitDecoratorMetadata and experimentalDecorators are be enabled in tsconfig.json

我注意到的一件事是,在查看侦查日志时,hooks是在Model.create而不是Model.updateOne上被调用的。

我如何使它们也可以在Update上工作

我的课

@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);

1 个答案:

答案 0 :(得分:0)

更新:找到一种更新方式,但未记录,我不推荐!:

        [HttpGet]
        public IActionResult downloadTextFile(string partialName)
        {
            downloadTextFileFromLocal(partialName);
            return null;
        }

        private static void downloadTextFileFromLocal(string partialName)
        {
            //string partialFileName = "2018-10-3";
            DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(@"D:\TextFile");
            FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + partialName + "*.*");
            foreach (FileInfo foundFile in filesInDir)
            {
                string fullName = foundFile.FullName;
                Console.WriteLine(fullName);
            }
        }