Skip to content

Commit

Permalink
fix; post init hooks using on() (Fix #2949)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed May 5, 2015
1 parent 043c3c0 commit 2d2c43f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/schema.js
Expand Up @@ -670,9 +670,8 @@ Schema.prototype.post = function(method, fn) {
}
// assuming that all callbacks with arity < 2 are synchronous post hooks
if (fn.length < 2) {
return this.queue('post', [arguments[0], function(next) {
fn.call(this, this);
next();
return this.queue('on', [arguments[0], function(doc) {
return fn.call(doc, doc);
}]);
}

Expand Down
20 changes: 20 additions & 0 deletions test/document.hooks.test.js
Expand Up @@ -561,4 +561,24 @@ describe('document: hooks:', function () {
db.close(done);
});
});

it('runs post hooks after function (gh-2949)', function(done) {
var schema = Schema({ name: String });

var postCount = 0;
schema.post('init', function(doc) {
assert.equal(doc.name, 'Val');
++postCount;
});

var db = start();
var People = db.model('gh-2949', schema, 'gh-2949');

People.create({ name: 'Val' }, function(err, doc) {
People.findOne({ _id: doc._id }, function(err) {
assert.equal(postCount, 1);
db.close(done);
});
});
});
});

0 comments on commit 2d2c43f

Please sign in to comment.