Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Consider this nesting setup:
// nested.js
var NestedPropDef = {
someProp: { type: String, default: '' },
};
var schema = new Schema(NestedPropDef, { minimize: false });
schema.statics = {
getInstance() {
var nestedObject = new NestedProp();
nestedObject.someProp = 'something';
console.log('[first log] value of nestedObject:', nestedObject);
return nestedObject;
},
});
var NestedProp = mongoose.model('NestedProp', schema);
exports.NestedPropDef = NestedPropDef;
exports.NestedProp = NestedProp;
// parent-file.js
var NestedPropDef = require('./nested').NestedPropDef;
var NestedProp = require('./nested').NestedProp;
var schema = new Schema({
otherProp: { type: String, default: '' },
nestedProp: NestedPropDef,
});
schema.methods.updateNestedProp = function (data, callback) {
this.nestedProp = NestedProp.getInstance();
console.log('[second log] value of nestedProp:', this.nestedProp);
this.otherProp: data.otherProp;
console.log('[third log] value of this:', this);
this.save(callback);
});
var Parent = mongoose.model('Parent', schema);
module.exports = Parent;
The current behavior is this:
[first log] value of nestedObject: {
someProp: 'something'
}
[second log] value of nestedProp: {}
[third log] value of this: {
otherProp: 'some value'
// nestedProp is missing
}
What is the expected behavior?
As of version 5.9.24, the log statements would be as follows:
[first log] value of nestedObject: {
someProp: 'something'
}
[second log] value of nestedProp: {
someProp: 'something'
}
[third log] value of this: {
otherProp: 'some value',
nestedProp: {
someProp: 'something'
}
}
I understand that using the schema of NestedProp
would _mostly_ restore the previous functionality, like this:
// nested.js
...
exports.NestedPropSchema = schema;
exports.NestedProp = NestedProp;
// parent-file.js
var NestedPropSchema = require('./nested').NestedPropSchema;
var NestedProp = require('./nested').NestedProp;
var schema = new Schema({
otherProp: { type: String, default: '' },
nestedProp: NestedPropSchema,
});
...
But this adds the _id
field to nestedProp
, which wasn't there previously. So the third log would be this:
[third log] value of this: {
otherProp: 'some value',
nestedProp: {
someProp: 'something',
_id: ...
}
}
Maybe that's a tiny and worthy sacrifice, and I know there's a way to disable the automatic addition of the _id
, but my objective is to restore the previous functionality so other subtle bugs don't creep up on me. How can I restore the results I was seeing in 5.9.24? Or was I always nesting incorrectly (and Mongoose was just working with my incorrect nesting)?
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
This is a bug in Mongoose, an unintended consequence of our fix for #9293 in v5.9.28. The fix will be in 5.10.4, sorry for the confusion!
@vkarpov15 awesome thanks! Looking forward to 5.10.4.
Most helpful comment
This is a bug in Mongoose, an unintended consequence of our fix for #9293 in v5.9.28. The fix will be in 5.10.4, sorry for the confusion!