Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I recreate previous behavior with nested path? #9392

Closed
CodeWithOz opened this issue Sep 5, 2020 · 2 comments
Closed

How can I recreate previous behavior with nested path? #9392

CodeWithOz opened this issue Sep 5, 2020 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@CodeWithOz
Copy link

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.

  • Node: 10 and 12
  • Mongoose: 5.10.3
@vkarpov15 vkarpov15 added this to the 5.10.4 milestone Sep 7, 2020
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 9, 2020
vkarpov15 added a commit that referenced this issue Sep 9, 2020
@vkarpov15
Copy link
Collaborator

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!

@CodeWithOz
Copy link
Author

@vkarpov15 awesome thanks! Looking forward to 5.10.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants