Skip to content

Commit

Permalink
Query#findOne callback is optional
Browse files Browse the repository at this point in the history
allows for more flexibility when passing queries around

closes Automattic#581
  • Loading branch information
aheckmann committed Oct 26, 2011
1 parent cfaa647 commit 02612f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,13 +843,16 @@ Query.prototype.each = function (callback) {
Query.prototype.findOne = function (callback) {
this.op = 'findOne';

if (!callback) return this;

var model = this.model;
var promise = new Promise(callback);

try {
this.cast(model);
} catch (err) {
return promise.error(err);
promise.error(err);
return this;
}

var self = this
Expand Down
11 changes: 11 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,17 @@ module.exports = {
setTimeout(db.close.bind(db), 300);
},

'#findOne should set the op when callback is passed': function () {
var db = start();
var query = new Query();
var Product = db.model('Product');
var q = new Query().bind(Product, 'distinct');
q.op.should.equal('distinct');
q.findOne();
q.op.should.equal('findOne');
db.close();
},

'querying/updating with model instance containing embedded docs should work (#454)': function () {
var db = start();
var Product = db.model('Product');
Expand Down

0 comments on commit 02612f7

Please sign in to comment.