Video 116: Cascade Delete Tasks
This commit is contained in:
parent
3a9e40b2f1
commit
9108ef7561
@ -2,6 +2,7 @@ const mongoose = require('mongoose')
|
|||||||
const validator = require('validator')
|
const validator = require('validator')
|
||||||
const bcrypt = require('bcryptjs')
|
const bcrypt = require('bcryptjs')
|
||||||
const jwt = require('jsonwebtoken')
|
const jwt = require('jsonwebtoken')
|
||||||
|
const Task = require('./task')
|
||||||
|
|
||||||
const userSchema = new mongoose.Schema({
|
const userSchema = new mongoose.Schema({
|
||||||
name: {
|
name: {
|
||||||
@ -102,6 +103,13 @@ userSchema.pre('save', async function(next) {
|
|||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Delete user tasks when user is removed
|
||||||
|
userSchema.pre('remove', async function(next) {
|
||||||
|
const user = this
|
||||||
|
await Task.deleteMany({ owner: user._id })
|
||||||
|
next()
|
||||||
|
})
|
||||||
|
|
||||||
const User = mongoose.model('User', userSchema)
|
const User = mongoose.model('User', userSchema)
|
||||||
|
|
||||||
module.exports = User
|
module.exports = User
|
||||||
|
Loading…
Reference in New Issue
Block a user