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

nested population getting null #1451

Closed
mrhooray opened this issue Apr 20, 2013 · 2 comments
Closed

nested population getting null #1451

mrhooray opened this issue Apr 20, 2013 · 2 comments

Comments

@mrhooray
Copy link

The mongoose I'm using is version 3.6.7.
Simply speaking, the model timeline has a yell which has a user.

I followed the gist mentioned in the release note to do nested population through 'Model.populate' but got null for for inner level population (timeline.yell.user => null). If I do the inner population document by document through 'Document.populate', I can get the expected result. If there any way to fix this?

doing population doc by doc:

  Timeline.find(req.query)
  .populate('recipient', 'username name email')
  .populate('yell')
  .exec(function(err, timelines){
    if (err) {
      res.send(400, {error: err});
    } else {
      async.map(timelines, function(timeline, callback){
        timeline.yell.populate(
          'user', 'username name email', function(err, yell){
          if (err) {
            callback(err);
          } else {
            callback();
          }
        });
      }, function(err, results){
        if (err) {
          return res.send(400, {error: err});
        } else {
          res.send(200, timelines);
        }
      });
    }
  });

get the expected result:

[
  {
    "recipient": {
      "username": "dummyuser1",
      "name": "dsafsdf",
      "email": "dummy1@user.com",
      "_id": "5171fb1be7c7d83bdd000001"
    },
    "yell": {
      "user": {
        "username": "dummyuser1",
        "name": "lebonn",
        "email": "dummy1@user.com",
        "_id": "517200dc02e90ccee3000001"
      },
      "content": "123",
      "radius": 123,
      "_id": "5172bff0432b640000000001",
      "__v": 0,
      "created": "2013-04-20T16:18:56.737Z",
      "location": {
        "type": "Point",
        "coordinates": [
          118.7667,
          32.05
        ]
      }
    },
    "_id": "5172bff0432b640000000002",
    "__v": 0,
    "created": "2013-04-20T16:18:56.748Z"
  }
]

doing nested population:

  Timeline.find(req.query)
  .populate('recipient', 'username name email')
  .populate('yell')
  .exec(function(err, timelines){
    if (err) {
      res.send(400, {error: err});
    } else {
      var opts = {path: 'yell.user'}; 
      Timeline.populate(timelines, opts, function(err, results){
        if (err) {
          res.send(400, {error: err});
        } else {
          res.send(200, results);
        }
      });
    }
  });

get null for yell.user

[
  {
    "recipient": {
      "username": "dummyuser1",
      "name": "dsafsdf",
      "email": "dummy1@user.com",
      "_id": "5171fb1be7c7d83bdd000001"
    },
    "yell": {
      "user": null,
      "content": "123",
      "radius": 123,
      "_id": "5172bff0432b640000000001",
      "created": "2013-04-20T16:18:56.737Z",
      "location": {
        "type": "Point",
        "coordinates": [
          118.7667,
          32.05
        ]
      }
    },
    "_id": "5172bff0432b640000000002",
    "__v": 0,
    "created": "2013-04-20T16:18:56.748Z"
  }
]
@teloo
Copy link

teloo commented Apr 21, 2013

@mrhooray
I think #1377 will help you.

@mrhooray
Copy link
Author

@teloo
Thanks. I need to specify the model for nested population.

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