1
0

Video 116: Cascade Delete Tasks

This commit is contained in:
JayWll 2020-07-16 11:32:56 -06:00
parent 3a9e40b2f1
commit 9108ef7561

View File

@ -2,6 +2,7 @@ const mongoose = require('mongoose')
const validator = require('validator')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const Task = require('./task')
const userSchema = new mongoose.Schema({
name: {
@ -102,6 +103,13 @@ userSchema.pre('save', async function(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)
module.exports = User