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

Passing undefined to mongoose's Model.create with vs without a callback #9765

Closed
OfirD1 opened this issue Jan 4, 2021 · 0 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@OfirD1
Copy link

OfirD1 commented Jan 4, 2021

Do you want to request a feature or report a bug?
bug

What is the current behavior?

I'm getting a different behavior when passing undefined to a mongoose's model's create when using vs. not using a callback.

steps to reproduce

An example:

const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({ name: { type: String, default: null } });
const User = mongoose.model('User', userSchema);
const user = undefined;

// compare this
User.create(user).then(createdUser => console.log(createdUser.name)); 

// with this
User.create(user, (err, createdUser) => console.log(createdUser.name)); 

In the first case, no User record is created in the database, and createdUser is undefined.
In the second case, a User record is created in the database, and createdUser is assigned.

  • Note that I'm aware of the following:

    In Mongoose 5, async operations [...] return a promise unless you pass a callback.

What is the expected behavior?
Both cases should create a record in the database and return the created record.

What are the versions of Node.js, Mongoose and MongoDB you are using?
Node.js: 12.16.1
MongoDB: 4.4.2
Mongoose: 5.11.9

@AbdelrahmanHafez AbdelrahmanHafez added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jan 5, 2021
@vkarpov15 vkarpov15 added this to the 5.11.11 milestone Jan 5, 2021
@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 Jan 6, 2021
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

No branches or pull requests

3 participants