Advertisement
SejimFU

Untitled

Dec 5th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Comment model
  2.  
  3. //***********************************************************************************
  4.  
  5. 'use strict'
  6.  
  7. var mongoose = require('mongoose');
  8. var Schema = mongoose.Schema;
  9. var Event = require('./event_model');
  10.  
  11. var User = require('./user_model');
  12.  
  13.  
  14. var commentSchema = new Schema({
  15.   content: String,
  16.   author: { type: Schema.Types.ObjectId, ref: 'User' },
  17.   event: { type: Schema.Types.ObjectId, ref: 'Event' }
  18. },
  19. {
  20.   timestamps: true //auto generation time of creation and last update
  21. });
  22.  
  23.  
  24. commentSchema.pre('remove', async function (next) {
  25.  
  26.   var comment = this;
  27.  
  28.   try {
  29.  
  30.     await Event.update({
  31.       comments: comment._id
  32.     }, {
  33.       $pull: {
  34.         comments: comment._id
  35.       }
  36.     });
  37.  
  38.     console.log('MIDDLEWARE REMOVE 1');
  39.  
  40.     await User.update({
  41.       comment: comment._id
  42.     }, {
  43.       $pull: {
  44.         comment: comment._id
  45.       }
  46.     });
  47.  
  48.     console.log('MIDDLEWARE REMOVE 2');
  49.  
  50.   //  next();
  51.   } catch (err) {
  52.     console.log('err');
  53.  
  54.     console.log(err);
  55.  
  56.     next(err)
  57.   }
  58. })
  59.  
  60. module.exports = mongoose.model('Comment', commentSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement