Skip to content

Commit

Permalink
fix(model): support calling create() with undefined as first argu…
Browse files Browse the repository at this point in the history
…ment and no callback

Fix #9765
  • Loading branch information
vkarpov15 committed Jan 6, 2021
1 parent dec79db commit dd132ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3000,7 +3000,7 @@ Model.create = function create(doc, options, callback) {
const last = arguments[arguments.length - 1];
options = {};
// Handle falsy callbacks re: #5061
if (typeof last === 'function' || !last) {
if (typeof last === 'function' || (arguments.length > 1 && !last)) {
cb = last;
args = utils.args(arguments, 0, arguments.length - 1);
} else {
Expand Down
10 changes: 9 additions & 1 deletion test/model.create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const DocumentObjectId = mongoose.Types.ObjectId;
*/

const schema = new Schema({
title: { type: String, required: true }
title: { type: String }
});

describe('model', function() {
Expand Down Expand Up @@ -187,6 +187,14 @@ describe('model', function() {
}).
catch(done);
});

it('treats undefined first arg as doc rather than callback (gh-9765)', function() {
return B.create(void 0).
then(function(doc) {
assert.ok(doc);
assert.ok(doc._id);
});
});
});
});
});

0 comments on commit dd132ee

Please sign in to comment.