Let's say I have 4 models like so:
┌────────────┐ ┌────────────┐
│ User │ │ Goal │
├────────────┤ 1 ├────────────┤ 1
│ _id │◄──┐ │ _id │◄──┐
└────────────┘ └──┤ ref_user │ │
1..*└────────────┘ │
│
┌───────────┐ ┌───────────────┐ │
│ Date │ │ Data │ │
├───────────┤ 1 ├───────────────┤ │
│ _id: date │◄─┐ │ _id │ │
└───────────┘ └──┤ ref_date │ │
1 │ ref_goal ├──┘
└───────────────┘ *
I created a pre remove when deleting a data it automatically removes the date associated.
DataSchema.pre('remove', async function (next) {
try {
await DateModel.findByIdAndRemove(this.ref_date);
console.log("Date removed");
next();
} catch (err) {
console.log(err);
next(err);
}
});
The issue I am facing is that when I remove a goal, I want all the Data associated to be deleted (it works) BUT I want the DataModel.remove to triggers its pre remove and remove its Date. In a logical perspective, when removing data on the pre remove hook in Goal, this also should trigger the pre remove in Data since Data data are removed. Why when removing a data from another pre remove hook it doesn't trigger its pre remove ?
From Goal, I can retreive an array of Data, but I don't think that looping through the array and removing one by one the date is a good practrice.
Is there a way to triggeres the pre remove from Data, from the pre remove from Goal ? Or a way to remove all the Date from an array of Data ?
ps: in the futur, I want the same thing again when deleting a user (deleting all its goals that deletes all the data and the date).
Edit :
As requested, he is my goal pre-remove hook
GoalSchema.pre('remove', async function (next) {
try {
await DataModel.deleteMany({ 'ref_goal': this._id });
console.log("Data removed");
next();
} catch (err) {
console.log(err);
next(err);
}
});
I tried deleteMany, remove, etc. but none seems to trigger the Data pre-remove hook
Env:
"express": "^4.17.1",
"mongoose": "^5.12.2",
from Express pre remove triggers another pre remove on remove
No comments:
Post a Comment