Created
March 25, 2021 16:16
-
-
Save blackmann/7c0889b354f5f7419036e00644f8a6c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is assuming the
ProductSchema
has the shop field as the id of the original shop