为什么"这个" mongoose预保存挂钩与后保存挂钩的关键字不同?

时间:2015-06-14 02:36:43

标签: node.js mongoose

这是我的代码的相关片段

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

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

出于某种原因,在这种情况下,预保存挂钩会提供适当的对象

{ 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] ] } }

但是帖子保存挂钩只是记录

{ domain: null,
  _events:
   { save: [ [Function: notify], [Function] ],
     isNew: [Function: notify],
     init: [Function] },
  _maxListeners: 0 }

1 个答案:

答案 0 :(得分:1)

post中间件接收文档作为参数,而不是next中间件接收的pre流控制回调参数。

MySchema
  .post('save', function(doc) {
    console.log(doc);
  });
相关问题