-
-
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 middleware of objects in array are never called (with 5.0.9) #6224
Comments
Hi @jmcollin78 Thanks for the thorough repro script! In your example you are calling .remove() on an embedded document. Which according to the docs is equivalent to calling pull on the subdocument. This does not fire a remove event. The subdocs .remove() middleware will only run when you call .remove() on the parent document. Here is an example:
output:
as you can see, the remove hooks only get run when you call remove on the parent document. but in both cases, remove and save, the subdoc's hooks get run first, then the parents. |
Many thanks for this ! If I remove a doc or a subdoc, I guess it should be removed, and subsequently, remove hooks should be called. Thanks for your explanation. |
@jmcollin78 I'm pretty new to mongoose and mongodb, rather than guess or make assumptions, I'll CC some folks that will have the answer. @vkarpov15 @varunjayaraman What is the reasoning behind only hooking a subdocument's remove when the parent is removed and not when the remove call is made on just the subdoc? I looked for existing open/closed bugs that reference this specific question, but if they exist I couldn't find them. Thanks! |
Because the alternative would be to make |
If I add a subdoc and call doc.save() the pre('save') callback is called. |
Hi there, I agree with @jmcollin78 : pre/post('remove') should be triggered on subdocuments. Here is why, each child has a file attached like so : const Child = new Schema({
filename: String
})
Child.post('remove', () => {
fs.unlink(this.filename)
})
const Parent = new Schema({
children: [Child]
}) So now, if I call But If I call So in my opinion it could be an improvement of the hook feature. What's your opinion @vkarpov15 ? |
Thx @pascallemoine, I feel so lonely with my problem.... |
@jmcollin78 did you ever get an adequate answer to your question? My project was using mongoose v4 and it supported post-remove middleware for subdocuments in the exact way you described. It wasn't until we upgraded to v5 that we lost the ability to run middleware whenever a subdocument was removed. Seems strange to me that they would remove that piece of functionality, but as far as I can tell, they have. |
For what it's worth, I noticed that mongoose uses kareem for middleware hooks which can be executed manually. There is a reference to kareem in the subdoc's schema (
|
@georgehess can you please open a new issue and follow the issue template? |
Do you want to request a feature or report a bug?
A bug
What is the current behavior?
I create an Embedded object named FileSpace into an array of object named Space. When removing FileSpace, pre remove middleware is never call (but pre validate middleware is called)
If the current behavior is a bug, please provide the steps to reproduce.
Here is a repro code:
The output is the following:
What is the expected behavior?
We should see the log line:
Please mention your node.js, mongoose and MongoDB version.
Node 9.5.0, Mongoose 5.0.9, MongoDB 3.6.3, Mongo driver: 3.0.3
EDIT: try with 5.0.9.
The text was updated successfully, but these errors were encountered: