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

Unable to set array to null as default #6691

Closed
sebastian-nowak opened this issue Jul 8, 2018 · 6 comments · Fixed by #14769
Closed

Unable to set array to null as default #6691

sebastian-nowak opened this issue Jul 8, 2018 · 6 comments · Fixed by #14769
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@sebastian-nowak
Copy link

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

@lineus
Copy link
Collaborator

lineus commented Jul 8, 2018

Hi @sebastian-nowak,

if you want a null value for the array default you need to return it from a function. Your example would become:

6691.js

#!/usr/bin/env node
'use strict';

const chai = require('chai');
const expect = chai.expect;
const mongoose = require('mongoose');

const schema = new mongoose.Schema({
  arrayThatShouldBeNull: {
    type: [String],
    default: () => { return 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; // now succeeds
expect(instance.arrayThatShouldContainTwoEntries).to.deep.equal(['asd', '123']); // passes

Output:

issues: ./6691.js
issues:

@lineus lineus closed this as completed Jul 8, 2018
@sebastian-nowak
Copy link
Author

Thanks for clarification. Is this behavior documented somewhere? I find it a bit surprising, I don't see any immediately obvious reason why null wouldn't work as a default value. Are there any other types that also required returning null via function?

@lineus
Copy link
Collaborator

lineus commented Jul 8, 2018

It's mentioned here in the FAQ, but I just noticed that the example needs to be changed there as the text says null but the example returns undefined.

@lineus lineus reopened this Jul 8, 2018
@lineus lineus added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Jul 8, 2018
@lineus
Copy link
Collaborator

lineus commented Jul 8, 2018

opened PR #6695 for this.

@lineus lineus closed this as completed Jul 8, 2018
@shashwat-idm
Copy link

this is still a thing in mongoose v7.
Why is this the default behaviour?
If i'm explicitly setting it to null, it should be null

@vkarpov15 vkarpov15 reopened this Jun 27, 2024
@vkarpov15 vkarpov15 added this to the 8.4.5 milestone Jun 27, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.4.5, 8.5 Jul 3, 2024
vkarpov15 added a commit that referenced this issue Jul 4, 2024
feat: allow setting array default value to `null`
@MiguelBrito1086
Copy link

MiguelBrito1086 commented Jul 17, 2024

@vkarpov15 is this actually working? I’m on v8.5.1 using
schemaKey: {type: [myArray], default: null, _id: false} and it fails. Validation error but works if I use a function returning null for default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants