Skip to content

Commit

Permalink
fix(query): don't call error handler if passRawResult is true and no …
Browse files Browse the repository at this point in the history
…error occurred

Fix #4836
  • Loading branch information
vkarpov15 committed Jan 2, 2017
1 parent d1492ce commit 175ad20
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
19 changes: 15 additions & 4 deletions lib/query.js
Expand Up @@ -1610,7 +1610,7 @@ function completeOne(model, doc, res, fields, pop, callback) {
return callback(err);
}
if (res) {
return callback(null, casted, res);
return callback(null, casted, decorateResult(res));
}
callback(null, casted);
});
Expand Down Expand Up @@ -1831,6 +1831,17 @@ Query.prototype._findOneAndRemove = function(callback) {
Query.base.findOneAndRemove.call(this, callback);
};

/*!
* ignore
*/

function decorateResult(res) {
if (res) {
res._kareemIgnore = true;
}
return res;
}

/**
* Override mquery.prototype._findAndModify to provide casting etc.
*
Expand Down Expand Up @@ -1923,7 +1934,7 @@ Query.prototype._findAndModify = function(type, callback) {

if (!doc || (utils.isObject(doc) && Object.keys(doc).length === 0)) {
if (opts.passRawResult) {
return callback(null, null, res);
return callback(null, null, decorateResult(res));
}
return callback(null, null);
}
Expand All @@ -1934,7 +1945,7 @@ Query.prototype._findAndModify = function(type, callback) {

if (!options.populate) {
return options.lean === true
? (opts.passRawResult ? callback(null, doc, res) : callback(null, doc))
? (opts.passRawResult ? callback(null, doc, decorateResult(res)) : callback(null, doc))
: completeOne(_this.model, doc, res, fields, null, callback);
}

Expand All @@ -1946,7 +1957,7 @@ Query.prototype._findAndModify = function(type, callback) {
}

return options.lean === true
? (opts.passRawResult ? callback(null, doc, res) : callback(null, doc))
? (opts.passRawResult ? callback(null, doc, decorateResult(res)) : callback(null, doc))
: completeOne(_this.model, doc, res, fields, pop, callback);
});
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"async": "2.1.4",
"bson": "~1.0.1",
"hooks-fixed": "1.2.0",
"kareem": "1.1.5",
"kareem": "1.2.0",
"mongodb": "2.2.16",
"mpath": "0.2.1",
"mpromise": "0.5.5",
Expand Down
22 changes: 22 additions & 0 deletions test/query.middleware.test.js
Expand Up @@ -262,4 +262,26 @@ describe('query middleware', function() {
done();
});
});

it('error handlers with findOneAndUpdate error and passRawResult (gh-4836)', function(done) {
var schema = new Schema({name: {type: String}});

var called = false;
var errorHandler = function(err, res, next) {
called = true;
next();
};

schema.post('findOneAndUpdate', errorHandler);

var Person = db.model('Person', schema);

Person.
findOneAndUpdate({}, {_id: 'test'}, {upsert: true, passRawResult: true}).
exec(function(error) {
assert.ok(error);
assert.ok(called);
done();
});
});
});

0 comments on commit 175ad20

Please sign in to comment.