Correct way to pass parameter to pre post hook?
See original GitHub issueI have read lots of issues from here and stackOverflow but still unable to find a relatively official way to pass required parameter to pre post hook.
For example, I am supposed to set password field by using findOneAndUpdate, and required to check if the new password has been applied before, if so then refuse to do the update.
I would like to do the check in pre(findOneAndUpdate’') hook, but there seems no way to pass my parameter to pre hook except this._update which contains the content to update,
Actually I can really do something like adding customName:'I am here'
in update clause, but looks weird, because update clause is purely to put field:value
, not proper place to put parameter.
Just try to see if anyone can help me confirm the official way to pass parameter to pre and post hook
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:11
I think @flyinghawker is trying to pass external parameters, sort of like trying to pass the
req
object, for checks inside the mongoose hooks.Ok so after a lot of effort the best way I could find was like so
await OrderModel.find({}, {test: 'test'})
Then in your pluginThe _fields object will have whatever you pass. So you could theoretically pass your request obj or session or whatever. The suck part is you have to pass your params every time your query. Would be nice to figure out a more global way to handle this for all models or schemas. This at least allows you to have a global plugin with schema and passed in data. I assume you could wrap the Model calls with something like withData or withSession, but would be nice to have something out of the box.