Mongoose - how to tap schema middleware into the 'init' event?

后端 未结 2 1912
感动是毒
感动是毒 2020-12-18 22:01

It is suggested in the Mongoose docs that I should be able to control the flow using middleware that plugs in to the \"init\" hook.

However, I have so far had succes

相关标签:
2条回答
  • 2020-12-18 22:25

    It turns out that the "init" event/hook is not fired when creating a new Model, it is only fired, when loading an existing model from the database. It seems that I should use the pre/validate hook instead.

    0 讨论(0)
  • 2020-12-18 22:47

    I have successfully used middleware like MySchema.post('init', function() { ... }); with Mongoose which is then executed for each model instance loaded in a find query. Note that there isn't a next parameter to call with this middleware, it should just return when done.

    0 讨论(0)
提交回复
热议问题