Skip to content

Commit

Permalink
fix: handle findOneAndRemove() with orFail()
Browse files Browse the repository at this point in the history
Fix #9381
  • Loading branch information
vkarpov15 committed Sep 6, 2020
1 parent dc1af4f commit 3261852
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/query.js
Expand Up @@ -4301,6 +4301,7 @@ Query.prototype.orFail = function(err) {
}
break;
case 'findOneAndDelete':
case 'findOneAndRemove':
if (get(res, 'lastErrorObject.n') === 0) {
throw _orFailError(err, this);
}
Expand Down
11 changes: 11 additions & 0 deletions test/model.findOneAndRemove.test.js
Expand Up @@ -347,6 +347,17 @@ describe('model: findOneAndRemove:', function() {
});
});

it('with orFail() (gh-9381)', function() {
const User = db.model('User', Schema({ name: String }));

return User.findOneAndRemove({ name: 'not found' }).orFail().
then(() => null, err => err).
then(err => {
assert.ok(err);
assert.equal(err.name, 'DocumentNotFoundError');
});
});

describe('middleware', function() {
it('works', function(done) {
const s = new Schema({
Expand Down

0 comments on commit 3261852

Please sign in to comment.