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

Default functions and 'this' #9351

Closed
ScottWager opened this issue Aug 22, 2020 · 2 comments
Closed

Default functions and 'this' #9351

ScottWager opened this issue Aug 22, 2020 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ScottWager
Copy link

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

Bug

What is the current behavior?

I have a key in my Schema that uses the default function to use this. When type is an array, this only includes default values of some of the keys:

If the current behavior is a bug, please provide the steps to reproduce.

authors: {
    default: function () {
        console.log(this);
    },
    type: [mongoose.Schema.Types.ObjectId],
},

Logs to console an object with only keys that have a default value

vs

authors: {
    default: function () {
        console.log(this);
    },
    type: mongoose.Schema.Types.ObjectId,
},

Logs to console an object that includes the keys that I'm setting in my 'createCourse' route

const course = await Course.create({
    title: req.body.title,
    publisher: req.account._id,
});

`What is the expected behavior?

Should have access to this.publisher when using an array.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js: 14.7.0
Mongoose: 5.10.0
MongoDB: 4.2.3

I love mongoose, thanks for the great work!

@vkarpov15 vkarpov15 added this to the 5.10.1 milestone Aug 22, 2020
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 24, 2020
vkarpov15 added a commit that referenced this issue Aug 24, 2020
@Eduwardo
Copy link

version 6.0.8 still have this problem, default function is running before the setters

@vkarpov15
Copy link
Collaborator

@Eduwardo please open a new issue and follow the issue template

@Automattic Automattic locked and limited conversation to collaborators Dec 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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