From d2c0e2af75549f0af3030f9e4cbabcba6b66a879 Mon Sep 17 00:00:00 2001 From: Mahmoud Ehab AbouDeghedy <67663696+Mahmoud-AbouDeghedy@users.noreply.github.com> Date: Fri, 21 Apr 2023 23:50:51 +0200 Subject: [PATCH] Update reviewModel.js why not use this simple line in one single method instead of that confusing way to access doc and model! --- 4-natours/after-section-11/models/reviewModel.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/4-natours/after-section-11/models/reviewModel.js b/4-natours/after-section-11/models/reviewModel.js index 9e3c93031d..d0d167a3b1 100644 --- a/4-natours/after-section-11/models/reviewModel.js +++ b/4-natours/after-section-11/models/reviewModel.js @@ -87,15 +87,8 @@ reviewSchema.post('save', function() { // findByIdAndUpdate // findByIdAndDelete -reviewSchema.pre(/^findOneAnd/, async function(next) { - this.r = await this.findOne(); - // console.log(this.r); - next(); -}); - -reviewSchema.post(/^findOneAnd/, async function() { - // await this.findOne(); does NOT work here, query has already executed - await this.r.constructor.calcAverageRatings(this.r.tour); +reviewSchema.post(/^findOneAnd/, async function (doc) { + await this.model.calcAverageRatings(doc.tour); }); const Review = mongoose.model('Review', reviewSchema);