From 9c7907f1e8c8e96295db4bb4311e1b6f062bfde9 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Fri, 1 Jun 2018 12:05:56 +0300 Subject: [PATCH] when deleting user, delete each todolist with .remove() --- models/User.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/models/User.js b/models/User.js index 0d88f2d..343ddf0 100644 --- a/models/User.js +++ b/models/User.js @@ -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 () {