New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Schema.pre('save') Not Working With Update Function #2672
Comments
update does a regular mongo driver call. Schema.update bypass the middleware by design. In the notes of the documentation to Model.update is defined what is not applyed.
If you want to get sure that your hooks are called, you have to get your model from DB (or hold it), edit it and use the save method on the model instance. |
|
Oh, thanks for the clarification. I thought that because I use |
This is misleading. pre('update') does not behave like pre('save') because the 'this' in the pre('update') is just the query and you cannot call useful functions like |
Right now a lot of people are following this code for password hashing https://stackoverflow.com/questions/14588032/mongoose-password-hashing The assumption I (and I would be many others have) is that every time a user saves their user record this Since we see here that this is not the case, if anyone changes their password, the password will not be salted and hashed. I guess the work around is to always first call a |
@ajbraus what if we added an official plugin? |
Sure @vkarpov15 - what does that entail? How do we get started? :D |
@ajbraus Mongoose has some officially supported plugins. They're separate projects that have functionality that we want to support but not necessarily put into mongoose core. If you're interested in writing a plugin, you're more than welcome to give it a shot: |
same issue and I done by solving it 'updateOne', and I update my answer here |
I was testing with
upsert
option when I realize,upsert
insert when called by Schema.update would not call schema.pre('save') middleware. Haven't done much test though.How to reproduce:
Use update function with upsert option.
On respective schema model, use some plugin.
Plugin pre('save') function wouldn't called.
I don't know if this is a bug, or there's some flaw in my code. Currently not affecting much my code, but some explanation would be appreciated. Thanks. :)
The text was updated successfully, but these errors were encountered: