Skip to content

Unable to set array to null as default #6691

Closed
@sebastian-nowak

Description

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

Bug in the latest mongoose version (5.2.1)

What is the current behavior?

Schemas ignore default: null for arrays, empty array is created instead.

const schema = new mongoose.Schema({
  arrayThatShouldBeNull: {
    type: [String],
    default: null,
    required: false
  },

  arrayThatShouldContainTwoEntries: {
    type: [String],
    default: ['asd', '123'],
    required: false
  }
}, {
  minimize: false // not sure if relevant
});

const Model = mongoose.model('test', schema);
const instance = new Model();

expect(instance.arrayThatShouldBeNull).to.be.null; // fails
expect(instance.arrayThatShouldContainTwoEntries).to.deep.equal(['asd', '123']); // passes

What is the expected behavior?

instance.arrayThatShouldBeNull should be set to null, but instead an empty array is created

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions