Skip to content

Commit 5c547ad

Browse files
committed
fix(query): report ObjectParameterError when passing non-object as filter to find() and findOne()
Fix #1698 Re: #4378
1 parent 0de9867 commit 5c547ad

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/query.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ Query.prototype._find = function(callback) {
13591359
*
13601360
* query.find({ name: 'Los Pollos Hermanos' }).find(callback)
13611361
*
1362-
* @param {Object} [criteria] mongodb selector
1362+
* @param {Object} [filter] mongodb selector
13631363
* @param {Function} [callback]
13641364
* @return {Query} this
13651365
* @api public
@@ -1375,9 +1375,11 @@ Query.prototype.find = function(conditions, callback) {
13751375

13761376
if (mquery.canMerge(conditions)) {
13771377
this.merge(conditions);
1378-
}
13791378

1380-
prepareDiscriminatorCriteria(this);
1379+
prepareDiscriminatorCriteria(this);
1380+
} else if (conditions != null) {
1381+
this.error(new ObjectParameterError(conditions, 'filter', 'find'));
1382+
}
13811383

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

16051607
if (mquery.canMerge(conditions)) {
16061608
this.merge(conditions);
1607-
} else if (conditions != null) {
1608-
throw new Error('Invalid argument to findOne(): ' +
1609-
util.inspect(conditions));
1610-
}
16111609

1612-
prepareDiscriminatorCriteria(this);
1610+
prepareDiscriminatorCriteria(this);
16131611

1614-
try {
1615-
this.cast(this.model);
1616-
this.error(null);
1617-
} catch (err) {
1618-
this.error(err);
1612+
try {
1613+
this.cast(this.model);
1614+
this.error(null);
1615+
} catch (err) {
1616+
this.error(err);
1617+
}
1618+
} else if (conditions != null) {
1619+
this.error(new ObjectParameterError(conditions, 'filter', 'findOne'));
16191620
}
16201621

16211622
if (!callback) {

0 commit comments

Comments
 (0)