Skip to content

Instantly share code, notes, and snippets.

@blackmann
Created March 25, 2021 16:16
Show Gist options
  • Save blackmann/7c0889b354f5f7419036e00644f8a6c2 to your computer and use it in GitHub Desktop.
Save blackmann/7c0889b354f5f7419036e00644f8a6c2 to your computer and use it in GitHub Desktop.
const ShopSchema = new Schema({ /** definitions */})
ShopSchema.pre('deleteOne', async function (shop) {
// Note, don't use arrow function, unless you realize the shop is passed
// to the middleware instance. Read below:
// right now the document is unclear whether it passes the instance
// of the object to the middleware or the `this` is the instance
// so to test, do this
console.log(shop)
console.log(this)
// depending on which one works, you know what to do
// then
await Product.deleteMany({shop: shop._id}) // Product model, not schema, note this too
})
const ProductSchema = new Schema({ /** definitions */ })
const Shop = mongoose.model('Shop', ShopSchema)
const Product = mongoose.model('Product', ProductSchema)
@blackmann
Copy link
Author

this is assuming the ProductSchema has the shop field as the id of the original shop

@divinecodes
Copy link

@Otoole311 take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment