官方文档代码:
var schema = new Schema(..);
schema.post('save', function (doc) {
console.log('this fired after a document was saved');
}); var Model = mongoose.model('Model', schema); var m = new Model(..);
m.save(function (err) {
console.log('this fires after the `post` hook');
});
大概意思是m在保存时会触发post监听的事件会被触发,
不明白post和pre有什么区别,
个人猜测,
1、触发的时间不同
pre发生在监听事件的前面;
post发生在在pre后面,但是好像也发生在监听事件的前面,但是比pre靠后,
UserSchema.post('save',function(doc){
console.log("post");
this.name="44444";
});
UserSchema.pre('save',function(next){
console.log("pre");
this.name=" "+"222";
console.log(this.name);
next();
});
输出结果为
/Users/Amanda/.nvm/versions/node/v4.3.1/bin/node bin/www
pre
222
post
save ok
OOO44444OOO