Skip to content

Commit

Permalink
fix: also allow setting nested field to undefined re: #14205
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 2, 2024
1 parent 66e5c1b commit c5fa9b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
} else {
throw new StrictModeError(key);
}
} else if (pathtype === 'nested' && path[key] === null) {
} else if (pathtype === 'nested' && path[key] == null) {
this.$set(pathName, path[key], constructing, options);
}
} else if (path[key] !== void 0) {
Expand Down
19 changes: 19 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10567,4 +10567,23 @@ describe('document', function() {
doc.set({ nested: null });
assert.strictEqual(doc.toObject().nested, null);
});

it('handles setting nested path to undefined (gh-14205)', function() {
const schema = new mongoose.Schema({
nested: {
key1: String,
key2: String
}
});

const Model = db.model('Test', schema);

const doc = new Model();
doc.init({
nested: { key1: 'foo', key2: 'bar' }
});

doc.set({ nested: void 0 });
assert.strictEqual(doc.toObject().nested, void 0);
});
});

0 comments on commit c5fa9b4

Please sign in to comment.