Skip to content

Commit

Permalink
fix(query): report ObjectParameterError when passing non-object as fi…
Browse files Browse the repository at this point in the history
…lter to find() and findOne()

Fix #1698
Re: #4378
  • Loading branch information
vkarpov15 committed Dec 1, 2017
1 parent 0de9867 commit 5c547ad
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ Query.prototype._find = function(callback) {
*
* query.find({ name: 'Los Pollos Hermanos' }).find(callback)
*
* @param {Object} [criteria] mongodb selector
* @param {Object} [filter] mongodb selector
* @param {Function} [callback]
* @return {Query} this
* @api public
Expand All @@ -1375,9 +1375,11 @@ Query.prototype.find = function(conditions, callback) {

if (mquery.canMerge(conditions)) {
this.merge(conditions);
}

prepareDiscriminatorCriteria(this);
prepareDiscriminatorCriteria(this);
} else if (conditions != null) {
this.error(new ObjectParameterError(conditions, 'filter', 'find'));
}

// if we don't have a callback, then just return the query object
if (!callback) {
Expand Down Expand Up @@ -1564,7 +1566,7 @@ Query.prototype._findOne = function(callback) {
* }
* });
*
* @param {Object|Query} [criteria] mongodb selector
* @param {Object} [filter] mongodb selector
* @param {Object} [projection] optional fields to return
* @param {Object} [options] see [`setOptions()`](http://mongoosejs.com/docs/api.html#query_Query-setOptions)
* @param {Function} [callback] optional params are (error, document)
Expand Down Expand Up @@ -1604,18 +1606,17 @@ Query.prototype.findOne = function(conditions, projection, options, callback) {

if (mquery.canMerge(conditions)) {
this.merge(conditions);
} else if (conditions != null) {
throw new Error('Invalid argument to findOne(): ' +
util.inspect(conditions));
}

prepareDiscriminatorCriteria(this);
prepareDiscriminatorCriteria(this);

try {
this.cast(this.model);
this.error(null);
} catch (err) {
this.error(err);
try {
this.cast(this.model);
this.error(null);
} catch (err) {
this.error(err);
}
} else if (conditions != null) {
this.error(new ObjectParameterError(conditions, 'filter', 'findOne'));
}

if (!callback) {
Expand Down

0 comments on commit 5c547ad

Please sign in to comment.