Skip to content

Commit

Permalink
Merge pull request #6614 from lineus/fix-6611
Browse files Browse the repository at this point in the history
fixes #6611 set saving to undefined before emitting save
  • Loading branch information
vkarpov15 committed Jun 24, 2018
2 parents 492c3fb + 0e0056e commit e140663
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Model.prototype.$__save = function(options, callback) {
});
}
}

this.$__.saving = undefined;
this.emit('save', this, numAffected);
this.constructor.emit('save', this, numAffected);
callback(null, this);
Expand Down
22 changes: 22 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5474,5 +5474,27 @@ describe('Model', function() {
test.save().then(handler);
test.save().catch(error);
});
it('allows calling save in a post save hook (gh-6611)', function() {
let called = 0;
const noteSchema = new Schema({
body: String
});

noteSchema.post('save', function(note) {
if (!called) {
called++;
note.body = 'a note, part deux.';
return note.save();
}
});


const Note = db.model('gh6611', noteSchema);
return co(function*() {
yield Note.create({ body: 'a note.' });
let doc = yield Note.findOne({});
assert.strictEqual(doc.body, 'a note, part deux.');
});
});
});
});

0 comments on commit e140663

Please sign in to comment.