0

I have this code

app.get('/json', function(req, res) {
        Bet.find({}, function(err, bets){
            if (err) throw err;
            Bet.populate(bets, { path: 'item' }, function(err, bets){
                if(err) throw err;
                Bet.populate(bets, { path: 'user' }, function(err, bets) {
                    if(err) throw err;
                    res.json(bets);
                });
            });
        });
    });

From this code i get json

{
   _id: "55e2ca9cc8b4b71821a6e90f",
   user: {
      _id: "55e2c9c3faa34a6d1f80ee93",
      __v: 0,
   steam: {
             avatar:"jpg",
            username: "Robert House",
          id: "76561198236425518"
         }
    },
itemsCount: 1,
__v: 0,
item: [
     {
       _id: "55e2ca9cc8b4b71821a6e910",
      type: "Rare mount",
     icon_url_large: "icon",
     icon_url: "icon",
     name: "Noctis the Heavenly Qilin Guardian",
      __v: 0
}
]
}

Then I try to insert this objects into one big document

Code that I already have

app.get('/gamesjson', function(req, res) {
        Game.find({}, function(err, games){
            if(err) throw err;
            Game.populate(games, { path: 'bets' }, function(err, games){
                Game.populate(games, { path: 'user' }, function(err, games) {
                    if(err) throw err;
                    res.json(games);
                });
             });
        });
    })

And I get this json:

_id: "55e2ca79c8b4b71821a6e90e",
__v: 0,
bets: [
 {
    _id: "55e2ca9cc8b4b71821a6e90f",
     user: "55e2c9c3faa34a6d1f80ee93",
     itemsCount: 1,
    __v: 0,
     item: [
       "55e2ca9cc8b4b71821a6e910"
     ]
  }
 ]
}

You can see that I don't get user object at all, only id, same happens with item object.

I think problem in path params, Game.populate(games, { path: 'user' } but I have no idea how this path should looks like.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471

0 Answers0