node.js - Why is the "this" keyword different in a mongoose pre-save hook versus a post-save hook? -


this relevant snippet of code

myschema   .pre('save', function (next) {     var self = this;     console.log(self);     return next();   });  myschema   .post('save', function (next) {     var self = this;     console.log(self);   }); 

for reason, in situation pre-save hook gives proper object

{ farm: 557ce790a893e4e0118059e3,   _id: 557ce791a893e4e011805a35,   privileges:    [ { instanceid: 557ce790a893e4e0118059bb,        access: 5,        modeltype: 'user' } ],   public: 0,   properties: { crop: 'no crop', name: 'pirani tract 50' },   geometry: { type: 'polygon', coordinates: [ [object] ] } } 

but post save hook logs

{ domain: null,   _events:    { save: [ [function: notify], [function] ],      isnew: [function: notify],      init: [function] },   _maxlisteners: 0 } 

post middleware receives document parameter instead of next flow control callback parameter pre middleware receive.

myschema   .post('save', function(doc) {     console.log(doc);   }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -