-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Pre-remove hooks not called for subdocuments #9885
Comments
@TimUnderhay appreciate you opening this ticket. Did it ever work for you? I finally upgraded to v8.2 and still can't get pre/post middleware to fire for subdocuments when they are deleted. |
@georgehess IIRC the issue was resolved (though much time has passed so my memory is a bit fuzzy) but I’m no longer using Mongoose so I can’t comment on whether that’s still the case. |
@georgehess |
@vkarpov15 looks like the supplied alternative (deleteOne) works with pre middleware. Post middleware doesn’t work but I guess that’s a whole other topic. |
@georgehess can you please provide code samples that clarify what you mean by "Post middleware doesn’t work but I guess that’s a whole other topic."? |
@vkarpov15 here's a mongoose playground example of what I mean. The pre deleteOne middleware for subdocuments runs as expected but post deleteOne never does. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Pre-remove middleware is not called when removing an item from an array of subdocuments. According to #6224, this seemed to work with Mongoose <5.0.0. @vkarpov15 asked @georgehess to open a new issue on this in the same #6224, though it appears not to have happened, thus I'm running it up the flagpole.
This behaviour presents a problem for me because I need to know when a document is removed from an array, so that a corresponding cache entry can be deleted. Unfortunately, modifiedPaths doesn't provide enough context to tell me when something is removed, only that something happened. I can, of course, clear the cache entry manually from outside of my Mongoose code, but this seems like a less-than-elegant solution.
If the current behavior is a bug, please provide the steps to reproduce.
Output from the above is:
What is the expected behavior?
Pre-remove hooks should run when removing sub-documents.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose 5.11.14
Node.js 15.5.1
MongoDB 4.4.1
The text was updated successfully, but these errors were encountered: