I am trying to deleteFeature meanwhile i want all the comments related to that feature deleted but i don't know how to do it.
my deleteFeature method -
exports.deleteFeature = (req, res) => {
try {
const { slug } = req.params;
Feature.findOne({ slug: slug.toLowerCase() }).exec((err, feature) => {
if (err) {
return res.status(400).json({
error: errorHandler(err),
});
}
console.log("Test");
Comment.deleteMany({ _id: feature._id });
console.log("chest");
feature.remove();
console.log("Best");
return res.json({
message: "Your Feature has been Deleted Successfully",
});
});
} catch (error) {
return res.status(400).json({
error: error,
});
}
};
I have this on comment model -
feature: {
type: ObjectId,
ref: "Feature",
required: true,
},
So when i delete a feature, i want to delete all the comments containing that feature's _id on that feature field
CodePudding user response:
Change
Comment.deleteMany({ _id: feature._id });
to
Comment.deleteMany({ feature: feature._id });