afterDestroy not called on adapter error
Similar to afterCreate and afterUpdate, we don't want to run the afterXXXX handlers if the delete failed according to the adapter.
This commit is contained in:
parent
13ac3cf088
commit
2ba862f5af
|
@ -721,8 +721,12 @@ AbstractClass.prototype.destroy = function (cb) {
|
||||||
|
|
||||||
this.trigger('destroy', function (destroyed) {
|
this.trigger('destroy', function (destroyed) {
|
||||||
this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
|
this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
|
||||||
|
if (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
|
||||||
destroyed(function () {
|
destroyed(function () {
|
||||||
if(cb) cb(err);
|
if(cb) cb();
|
||||||
});
|
});
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
});
|
});
|
||||||
|
|
|
@ -221,6 +221,22 @@ describe('hooks', function() {
|
||||||
user.destroy();
|
user.destroy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('afterDestroy should not be triggered on failed destroy', function(done) {
|
||||||
|
var old = User.schema.adapter.destroy;
|
||||||
|
User.schema.adapter.destroy = function(modelName, id, cb) {
|
||||||
|
cb(new Error('error'));
|
||||||
|
}
|
||||||
|
User.afterDestroy = function() {
|
||||||
|
throw new Error('shouldn\'t be called')
|
||||||
|
};
|
||||||
|
User.create(function (err, user) {
|
||||||
|
user.destroy(function(err) {
|
||||||
|
User.schema.adapter.destroy = old;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('lifecycle', function() {
|
describe('lifecycle', function() {
|
||||||
|
|
Loading…
Reference in New Issue