0

Trying to populate an array of ObjectID's within my schema. I've looked around at similar answers but it seems everyone is doing it slightly differently and I haven't been able to find a solution myself.

My schema looks like this:

var GameSchema = new Schema({
  title: String,
  description: String,
  location: String,
  created_on: { type: Date, default: Date.now },
  active: { type: Boolean, default: true },
  accepting_players: { type: Boolean, default: true },
  players: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
  }],
  admins: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
  }]
});

So far I've been trying to populate it like this, which obviously isn't working

exports.getAdmins = function(req, res) {
  Game.findById(req.params.id)
  .populate('admins')
  .exec(function(err, game) {
    return res.json(200, game.admins);
  });
};

I hate to add to the list of population questions, but I've looked at many and haven't found a solution. Any help is greatly appreciated!

Edit: Here's how I am adding admins to the document

// Add admin to game
exports.addAdmin = function(req, res) {
  Game.findByIdAndUpdate(
    req.params.id,
    { $push: { 'admins': req.params.user_id }},
    function(err, game) {
      if(err) { return handleError(res, err); }
      if(!game) { return res.send(404); }
      return res.json(200, game.admins);
    });
};
jket
  • 418
  • 6
  • 9
  • can you give an example of how you are saving your game objects, this should work, just attempted the same structure on my end in a test app and it worked fine. – Mattias Farnemyhr Sep 11 '14 at 20:40
  • @materik Edited the code for how I add admins to the game. When I get the game document from the API all the data is correct and I can see the admin ID's saved in the array. Let me know if I answered your question. – jket Sep 12 '14 at 01:30
  • Hmm.. yes, that looks good. What version of mongodb are you running. I don't know if that makes a difference but I remember I had a problem with populate before I upgraded version. – Mattias Farnemyhr Sep 12 '14 at 07:57
  • @materik I was on version 2.6.0 so I brew upgraded to 2.6.4 but that didn't fix the problem at first. However I looked through some more mongoose documentation and got it working, I'll post the solution I found in the answer. Thanks for the help! – jket Sep 12 '14 at 16:45

2 Answers2

0

Well I went back to mongoose documentation, and decided to change how I looked up a game by an ID and then populated the response.

Now my working function looks like this:

// Returns admins in a game
exports.getAdmins = function(req, res) {
  Game.findById(req.params.id, function(err, game) {
    if(err) { return handleError(res, err); }
    if(!game) { return res.send(404); }
    Game.populate(game, { path: 'admins' }, function(err, game) {
      return res.json(200, game);
    });
  });
};

The issue I was having was that I was trying to call the .populate function directly with the .findById method, but that doesn't work because I found on mongoose's documentation the the populate method need the callback function to work, so I just added that and voila, it returned my User object.

jket
  • 418
  • 6
  • 9
0

to populate an array, you just have to put model name field after path field like this :

 Game.findById(req.params.id)
         .populate({path: 'admins', model: 'AdminsModel'})
         .exec(function(err, game){...});

it works perfectly on my projects...

slim
  • 447
  • 2
  • 10
  • 27