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

Model.create fails with Cannot read property 'length' of undefined if there is a second argument with value null or undefined. #13487

Closed
2 tasks done
robincsamuel opened this issue Jun 7, 2023 · 2 comments · Fixed by #13493
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@robincsamuel
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.2.2

Node.js version

14.20.1

MongoDB server version

5.0.7

Typescript version (if applicable)

No response

Description

The function throws the following error when executing Model.create with a second argument having value null or undefined,

TypeError: Cannot read property 'length' of undefined
    at Function.create (/Users/robin/Projects/sample/node_modules/mongoose/lib/model.js:2825:14)

It works fine on version 6.x.

Steps to Reproduce

const User = model(
  'User',
  new Schema({
    name: String,
  }),
);

const createUser = (payload, options=undefined) => {
   return User.create(payload, options)
}

await createUser({name: "Robin"})

Expected Behavior

The entry should get created. The options parameter should be ignored since it's undefined.be ignored

@vkarpov15 vkarpov15 added this to the 7.2.3 milestone Jun 8, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jun 8, 2023
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jun 9, 2023
@vkarpov15
Copy link
Collaborator

We'll fix the unexpected error in #13493, but please be aware that your syntax for create() is wrong. Calling create() with 2 object arguments creates 2 documents, Mongoose does not treat the 2nd arg as options unless the first arg is an array.

Please use the following instead:

const createUser = (payload, options=undefined) => {
   return User.create([payload], options) // <-- array around `payload` to make Mongoose use 2nd arg as options
}

vkarpov15 added a commit that referenced this issue Jun 9, 2023
fix(model): ignore falsy last argument to create() for backwards compatibility
@robincsamuel
Copy link
Author

robincsamuel commented Jun 9, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants