Skip to content

nested population getting null #1451

Closed
Closed
@mrhooray

Description

@mrhooray

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"
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions