I am working on appointment booking app in nodejs
I have 2 schemas one is slot schema which stores the slots (startTime-endTime) and one subSlot Schema which stores the chunks of (StartTime-endTime)
for ex - (10 - 12) is slot range then subslot contains subSlot - (10:00,10:30,11:00,11:30,12;00)
I want to write a login where if we delete slot all its subslot gets deleted automatically
here what i have tried so far but its not working at all. .remove function is throwing error
router.get("/deleteSlot/:id", (req, res) => {
slot.findByIdAndRemove(req.params.id, (err, doc) => {
if (!err) {
subSlotBooking.find({ slots: req.params.id }, (err, docs) => {
if (!err) {
docs.remove((err) => {
if (!err) {
console.log("docs eleted successfully");
}
});
}
});
res.redirect("/displaySlot");
} else {
req.flash("error_msg", "Failed to delete");
}
});
});
here is my subslot schemas
const mongoose = require("mongoose");
const slotBookingSchema = new mongoose.Schema({
startTime: [{ type: String }],
endtime: { type: String },
isBooked: { type: Boolean, default: false },
isDisabled: { type: Boolean, default: false },
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
slots: { type: mongoose.Schema.Types.ObjectId, ref: "slot" },
});
const subSlotBooking = mongoose.model("subSlotBooking", slotBookingSchema);
module.exports = subSlotBooking;
main slot schema
const mongoose = require("mongoose");
const { mongo } = require("mongoose");
const subSlotBooking = require("../models/slotBooking.model");
const slotSchema = new mongoose.Schema(
{
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
docdetail: {
type: mongoose.Schema.Types.ObjectId,
ref: "Doctor",
},
startTime: {
type: String,
},
endtime: {
type: String,
},
interval: {
type: String,
},
},
{ timestamps: {} }
);
slotSchema.pre("remove", function (next) {
// 'this' is the client being removed. Provide callbacks here if you want
// to be notified of the calls' result.
subSlotBooking.remove({ slots: this._id }).exec();
next();
});
const slot = mongoose.model("slot", slotSchema);
module.exports = slot;
I also tried the answer given here
but not working.
How do I do this?