Skip to content

Commit b286b02

Browse files
committed
fix: also allow setting nested field to undefined re: #14205
1 parent 6d526cd commit b286b02

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
11531153
} else {
11541154
throw new StrictModeError(key);
11551155
}
1156-
} else if (pathtype === 'nested' && valForKey === null) {
1156+
} else if (pathtype === 'nested' && valForKey == null) {
11571157
this.$set(pathName, valForKey, constructing, options);
11581158
}
11591159
} else if (valForKey !== void 0) {

test/document.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12456,6 +12456,25 @@ describe('document', function() {
1245612456
doc.set({ nested: null });
1245712457
assert.strictEqual(doc.toObject().nested, null);
1245812458
});
12459+
12460+
it('handles setting nested path to undefined (gh-14205)', function() {
12461+
const schema = new mongoose.Schema({
12462+
nested: {
12463+
key1: String,
12464+
key2: String
12465+
}
12466+
});
12467+
12468+
const Model = db.model('Test', schema);
12469+
12470+
const doc = new Model();
12471+
doc.init({
12472+
nested: { key1: 'foo', key2: 'bar' }
12473+
});
12474+
12475+
doc.set({ nested: void 0 });
12476+
assert.strictEqual(doc.toObject().nested, void 0);
12477+
});
1245912478
});
1246012479

1246112480
describe('Check if instance function that is supplied in schema option is availabe', function() {

0 commit comments

Comments
 (0)