【问题标题】:How can i access save options inside a post hook in mongoose?如何在猫鼬的帖子挂钩内访问保存选项?
【发布时间】:2021-06-27 01:26:37
【问题描述】:

在 mongoose 模式中间件中,有 pre hooks 和 post hooks。如果我使用“保存”中间件,我可以传递选项并访问 pre hook 中的那些 optinos

//in controller
const doc = new someSchema({name:"Jeff"})
doc.save({someOption:true})
----
someSchema.pre('save',function(next,opts){
  console.log(opts) // logs {someOption:true} along with all doc options
}

我希望能够使用在保存时触发的 post hook 做同样的事情,但我找不到访问这些选项的方法

 someSchema.post('save',function(doc,next){
   // How do i access the options? I can't find them anywhere
   //if i log the doc, i only get the actual doc data, in this case name:jeff, 
  // is there a way to also see all of the getters and options if i log it?
}

【问题讨论】:

    标签: node.js mongoose schema hook middleware


    【解决方案1】:

    这是我访问它的方式(虽然在 pre hook 上):

    this.$__.saveOptions
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-08
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 2017-08-19
      • 1970-01-01
      • 2021-07-05
      相关资源
      最近更新 更多