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

enabling virtual populate in sub-doc schema results in null in find #4683

Closed
tarun1793 opened this issue Nov 4, 2016 · 3 comments
Closed
Milestone

Comments

@tarun1793
Copy link
Contributor

tarun1793 commented Nov 4, 2016

mongoose: 4.6.6
mongodb: 2.2.11
node: 4.5

Just enabling/disabling the virtual populate field results in array subdocuments showing as null in query results

var someModelSchema = new mongoose.Schema({
  name: String
});

mongoose.model('someModel', someModelSchema)
var schema0 = new mongoose.Schema({
  name0: String
});

schema0.virtual('detail', {
  ref: 'SomeModel',
  localField: '_id',
  foreignField: '_id',
  justOne: true
});

var schema1 = new mongoose.Schema({
  name1: String,
  list: [schema0],
  obj: schema0
});

var schemaMain = new mongoose.Schema({
  nestedObj: schema1,
  nameMain: String
});
var ModelMain = mongoose.model('ModelMain', schemaMain);

var x = new ModelMain({
  nameMain: 'sss',
  nestedObj: {
    name1: 'name1',
    list: [{
      name0: 'name0.1'
    }],
    obj: {
      name0: 'name0.1'
    }
  }
})
x.save().then(function (err, docs) {
  ModelMain.find({}, function (err, docs) {
    console.log(JSON.stringify(docs, null, 4));
  })
})

results in

 {
        "_id": "581cbf70d7c120443684a490",
        "nameMain": "sss",
        "nestedObj": {
            "name1": "name1",
            "obj": {
                "name0": "name0.1",
                "_id": "581cbf70d7c120443684a492"
            },
            "_id": "581cbf70d7c120443684a491",
            "list": [
                null
            ]
        },
        "__v": 0
    }

the list property is null. If you comment out the virtual property declaration then list contains expected data, which is:

 {
        "_id": "581cbfdf0700ba445223c848",
        "nameMain": "sss",
        "nestedObj": {
            "name1": "name1",
            "obj": {
                "name0": "name0.1",
                "_id": "581cbfdf0700ba445223c84a"
            },
            "_id": "581cbfdf0700ba445223c849",
            "list": [
                {
                    "name0": "name0.1",
                    "_id": "581cbfdf0700ba445223c84b"
                }
            ]
        },
        "__v": 0
    }
@tarun1793
Copy link
Contributor Author

This problem is no longer there after commenting out the pre-hook in Schema.prototype.virtual declaration in schemas.js
Even after replacing the hook with an empty one:

    this.pre('init', function(next, obj) {
      next();
    });

the issue is still there.
All is fine if this is commented out.

@vkarpov15
Copy link
Collaborator

#1079 strikes again. Put out a fix and will be out with 4.6.8 👍

@vkarpov15
Copy link
Collaborator

Fixed in d09c3d6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants