postSchema.post('save', function (doc, next) { if (this.wasNew) this.vote(this.author._id, 1); doc .populate('author') .populate('comments.author', '-role') .execPopulate() .then(() => next()); });
UserSchema.post('validate', doc => { if (doc.isModified('password')) { doc.password = CryptUtils.encrypt(doc.password); } if (!doc._id) { doc.creation_date = Date.now(); } });
FileSchema.post('remove', async function onRemove(doc, next) { await Promise.all([ Firebase.storage.deleteFile(doc.fileName) ]); next(); });
BusinessUserSchema.post('remove', function onRemove(doc, next) { const Business = mongoose.model('Business'); Promise.all([Business.removeByOwner(doc)]) .then(() => next()) .catch(next); });
followSchema.post('save', function(doc) { if (doc.wasNew) { var userId = doc.user._id || doc.user; var targetId = doc.target._id || doc.target; FeedManager.followUser(userId, targetId); } });
reviewSchema.post(/^findOneAnd/, async function() { // await this.findOne(); does NOT work here, query has already executed await this.r.constructor.calcAverageRatings(this.r.tour); });
OfferSchema.post('remove', async function onRemove(doc, next) { await Promise.all([ OfferLocation.removeByOffer(doc), FavouriteOffer.removeByOffer(doc), OfferViewsSchema.removeByOffer(doc), storageCtrl.deleteImagesReferences(doc._doc, possibleImageUrlsFields) ]); next(); });
tourSchema.post(/^find/, function(docs, next) { console.log(`Query took ${Date.now() - this.start} milliseconds!`); next(); });
followSchema.post('remove', function(doc) { FeedManager.unfollowUser(doc.user, doc.target); });
// Average Cost after save ReviewSchema.post("save", function() { this.constructor.getAverageRating(this.bootcamp); });
BusinessSchema.post('remove', function postRemove(doc, next) { const Offer = mongoose.model('Offer'); return Promise.all([ Offer.removeByBusiness(doc), storageCtrl.deleteImagesReferences(doc._doc, possibleImageUrlsFields) ]) .then(() => next()) .catch(next); });
// Do some thing after saving data User.post('save', function(obj, next) { logger.debug('Finished %s', obj._id.toString()); next(); });
userSchema.post('validate', function (doc) { logger.info('User has been validated (but not saved yet). ID: ' + doc._id); });
reviewSchema.post('save', function() { // this points to current review this.constructor.calcAverageRatings(this.tour); });
// Average Cost after save CourseSchema.post("save", function() { this.constructor.getAverageCost(this.bootcamp); });