I am trying to use a mongoose middleware to recursively delete references to this object in another objects array (delete task
references in taskList
with the id from task
)
The problematic function:
TaskSchema.pre('deleteOne', function(next) {
console.log(this)
const task: any = this
this.model('Set').deleteMany({ $pullAll: { taskList: Types.ObjectId(task.id.toString()) } }, next)
})
This throws an ObjectParameterError: Parameter "obj" to Document() must be an object when called by
const task = await this.taskSchema.deleteOne({ _id: id })
Further an issue is that in the last line this.model
=> I don't use model anywhere since instead of declaring a model i use:
export type TaskDocument = Task & Document
export const TaskSchema = SchemaFactory.createForClass(Task)
Task Object:
Set Object: