question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct way to pass parameter to pre post hook?

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

2reactions
Ncifracommented, Mar 25, 2019

I think @flyinghawker is trying to pass external parameters, sort of like trying to pass the req object, for checks inside the mongoose hooks.

0reactions
jberrydevcommented, May 18, 2022

Ok so after a lot of effort the best way I could find was like so await OrderModel.find({}, {test: 'test'}) Then in your plugin

export function myPlugin(schema: Schema, options: any) { 
 schema.pre(['find', 'findOne'], function () {
    const { _fields } = this as any;
    console.log('PLUGIN', _fields, schema);
  });
}

The _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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongoose hook access original arguments - Stack Overflow
I want to hash my users password on creation of an account in mongoose, is set up a hook to asynchrounously hash the...
Read more >
pre-hook & post-hook - dbt Developer Hub
In older versions of dbt, the most common use of post-hook was to execute grant statements, to apply database permissions to models right...
Read more >
Ubuntu Manpage: githooks - Hooks used by Git
It takes no parameters, and is invoked after the merge has been carried out successfully and before obtaining the proposed commit log message...
Read more >
CC Tutorial4 - Pre and Post Hooks | brainCloud API Docs
First write the cloud code script with the appropriate parameter/return syntax (see below, or the API Reference); Then attach the cloud code script...
Read more >
what is correct way to hook when update post
' save_post ' that is fired both on update and on first saving, and pass the same 3 arguments of the previous hook....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found