Skip to content

Commit e6918a4

Browse files
authored
Merge pull request #14769 from Automattic/vkarpov15/gh-6691-2
fix: allow setting document array default to `null`
2 parents f921c9a + 9a2462c commit e6918a4

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/schema/documentArray.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function SchemaDocumentArray(key, schema, options, schemaOptions) {
6969

7070
const fn = this.defaultValue;
7171

72-
if (!('defaultValue' in this) || fn !== void 0) {
72+
if (!('defaultValue' in this) || fn != null) {
7373
this.default(function() {
7474
let arr = fn.call(this);
7575
if (arr != null && !Array.isArray(arr)) {

test/document.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -3203,16 +3203,22 @@ describe('document', function() {
32033203
names: {
32043204
type: [String],
32053205
default: null
3206+
},
3207+
tags: {
3208+
type: [{ tag: String }],
3209+
default: null
32063210
}
32073211
});
32083212

32093213
const Model = db.model('Test', schema);
32103214
const m = new Model();
32113215
assert.strictEqual(m.names, null);
3216+
assert.strictEqual(m.tags, null);
32123217
await m.save();
32133218

32143219
const doc = await Model.collection.findOne({ _id: m._id });
32153220
assert.strictEqual(doc.names, null);
3221+
assert.strictEqual(doc.tags, null);
32163222
});
32173223

32183224
it('validation works when setting array index (gh-3816)', async function() {

0 commit comments

Comments
 (0)