Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Model.create failing when the last argument is falsy #13491

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2796,11 +2796,17 @@ Model.findByIdAndRemove = function(id, options) {
*/

Model.create = async function create(doc, options) {
if (typeof options === 'function' ||
typeof arguments[2] === 'function') {
const argumentsArray = Array.from(arguments);
const argumentsArrayAfterTheFirst = argumentsArray.slice(1);

if (argumentsArray.some(argument => typeof argument === 'function')) {
throw new MongooseError('Model.create() no longer accepts a callback');
}

if (arguments.length > 1 && argumentsArrayAfterTheFirst.some(argument => !argument)) {
throw new MongooseError('Passed a falsy value to Model.create() after doc');
}

_checkContext(this, 'create');

let args;
Expand All @@ -2811,15 +2817,10 @@ Model.create = async function create(doc, options) {
options = options != null && typeof options === 'object' ? options : {};
} else {
const last = arguments[arguments.length - 1];

options = {};
if (typeof last === 'function' || (arguments.length > 1 && !last)) {
if (typeof options === 'function' ||
typeof arguments[2] === 'function') {
throw new MongooseError('Model.create() no longer accepts a callback');
}
} else {
args = [...arguments];
}

args = [...arguments];

if (args.length === 2 &&
args[0] != null &&
Expand Down