mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Default fields in embedded document are overwritten with undefined

Open vitorhnn opened this issue 6 years ago • 0 comments

Do you want to request a feature or report a bug? Bug What is the current behavior? Passing undefined as a value to a key in a embedded document causes Mongoose to first set the key to its default value and then to overwrite it with undefined If the current behavior is a bug, please provide the steps to reproduce.

it('Sets default when passing undefined as value for a key in a nested subdoc (gh-9039)', function () {
  const Test = db.model('Test', {
    nested: {
      prop: {
        type: String,
        default: 'some default value'
      }
    }
  });

  return co(function* () {
    const doc = yield Test.create({ nested: { prop: undefined } });
    assert.equal(doc.nested.prop, 'some default value');
  });
});

What is the expected behavior? See the provided test

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Node: 14.2.0 Mongoose: 5.9.15 MongoDB: 3.6.17

vitorhnn avatar May 20 '20 18:05 vitorhnn