-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
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:
|
Thanks for clarification. Is this behavior documented somewhere? I find it a bit surprising, I don't see any immediately obvious reason why |
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. |
opened PR #6695 for this. |
this is still a thing in mongoose v7. |
feat: allow setting array default value to `null`
@vkarpov15 is this actually working? I’m on v8.5.1 using |
fix: allow setting document array default to `null`
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.What is the expected behavior?
instance.arrayThatShouldBeNull
should be set to null, but instead an empty array is createdThe text was updated successfully, but these errors were encountered: