[Vuejs]-How to delete office from Departments when I delete Office

0👍

Try to declare a pre middleware on the Office schema:

Office.pre('findOneAndDelete', function(next) {
    Department.updateMany({ offices: this._id }, {
        $pull: { offices: this._id }
    }).exec();
    next();
});

0👍

Mistake was here

const officeModel = mongoose.model("Offices", Office);
module.exports = officeModel;

"Offices" needed to be "Office" 😀

Leave a comment