diff --git a/lib/base-document.js b/lib/base-document.js index 3877a52..4e47f20 100644 --- a/lib/base-document.js +++ b/lib/base-document.js @@ -538,7 +538,7 @@ class BaseDocument { var hookPromises = []; hookPromises = hookPromises.concat(_.invoke(embeddeds, hookName)); - hookPromises.push(_.invoke([this], hookName)); + hookPromises.push(this[hookName]()); return hookPromises; } } diff --git a/test/issues.test.js b/test/issues.test.js index fd1a1f1..861a9b7 100644 --- a/test/issues.test.js +++ b/test/issues.test.js @@ -226,4 +226,34 @@ describe('Issues', function() { }).then(done, done); }); }); + + describe('#57', function() { + it('should not save due to Promise.reject in hook', function(done) { + /* + * Rejecting a Promise inside of a pre-save hook should + * cause the save to be aborted, and the .caught() method + * should be invoked on the Promise chain. This wasn't + * happening due to how the hooks were being collected + * and executed. + */ + + class Foo extends Document { + constructor() { + super(); + + this.bar = String; + } + + preValidate() { + return Promise.reject('DO NOT SAVE'); + } + } + + Foo.create({bar: 'bar'}).save().then(function(foo) { + expect.fail(null, Error, 'Expected error, but got none.'); + }).catch(function(error) { + expect(error).to.be.equal('DO NOT SAVE'); + }).then(done, done); + }); + }); }); \ No newline at end of file