Skip to content

'this' equals 'global' when a property's type is an Array #6263

@cocoasoda

Description

@cocoasoda

Do you want to request a feature or report a bug?
bug
What is the current behavior?
define a Schema like below

const Group = new mongoose.Schema({
    title: {
        type: String,
        required: true
    },
    members: {
        type: [
            { type: SchemaTypes.ObjectId, ref: 'User' }
        ],
        default: []
    },
    owner: {
        type: SchemaTypes.ObjectId,
        ref: 'User',
        required: true
    },
    managers: {
        type: [{
            type: SchemaTypes.ObjectId,
            ref: 'User'
        }],
        /* type: String works fine but not type: [String] */
        default: function() {
            console.log('this:',this === global);
            return [this.owner];
        }
    }
})

then create a document

const g = new Group({
        title: 'test',
        owner: users[0]._id,
    });

the document's members property is [ undefined ]
If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
'this' should be the document itself, but this === global is true
Please mention your node.js, mongoose and MongoDB version.
nodejs v9.8.0 (Windows)
mongoose 5.0.11
mongoDB runs on Azure Cosmos DB maybe ^v3.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions