when deleting user, delete each todolist with .remove()

This commit is contained in:
2018-06-01 12:05:56 +03:00
parent e49eb79d45
commit 9c7907f1e8

View File

@@ -22,15 +22,10 @@ UserSchema.plugin(passportLocalMongoose);
UserSchema.plugin(uniqueValidator);
UserSchema.pre('remove', async function () {
await this.model('TodoList')
const lists = await this.model('TodoList')
.find({ user: this._id })
.remove()
.exec();
await this.model('Todo')
.find({ user: this._id })
.remove()
.exec();
await Promise.all(lists.map(list => list.remove()));
});
UserSchema.methods.generateJwt = function () {