I've been trying to solve this issue for about 4 hours now, and I can't find a way to solve it. I have 3 Models, that look like this:
The 1st one is my Tournament model:
var tournamentSchema = mongoose.Schema({
tournamentName: String,
nrOfPlayers: String,
players: [{
type: mongoose.Schema.Types.ObjectId, ref: "User" }],
openForLeagues: {
"leagues" : []
},
organizer: [{
type: mongoose.Schema.Types.ObjectId, ref: "User"
}]
});
The 2nd one is my User model:
var userSchema = mongoose.Schema({
local : {
nickname: String,
battlenetid: String,
email: String,
password: String,
race: String,
league: String,
role: String,
website: String,
avatarImage: String,
tournaments: [{
type: mongoose.Schema.Types.ObjectId, ref: "Tournament"
}],
avatar: [{
type: mongoose.Schema.Types.ObjectId, ref: "Avatar"
}]
}
});
And the 3rd one is my Avatar model:
var avatarSchema = mongoose.Schema({
imageName: String,
imageRaceCategory: String,
imagePath: String
});
What i am trying to do is to find a Tournament based on it's id, then populate the players array and also the avatar for each of the player that is taking part in this tournament. However, I get undefined when i try to display the avatars for the players.
This is how my query looks like:
Tournament.findById(req.params._id).populate('players organizer players.local.avatar').exec( function(err, tournament){
if(err){
res.send(err)
}else{
helperFunctions.getUserDetails(req.params.userId, function(user){
res.render('tournament/tournament-details.ejs',{
user: req.user,
tournament: tournament,
userAvatar: user,
moment: moment,
enlistedInTournament: enlistedInTournament,
eligibleForTournament: eligibleForTournament,
allPlacesTaken: allPlacesTaken,
procentajOcupare: (tournament.players.length * (100 / tournament.nrOfPlayers))
});
});
}
});
And this is how i display it in the view:
<%tournament.players.forEach(function(player){%>
<img src="<%=player.local.avatar.imagePath + '/' + player.local.avatar.imageName"%>/>
<li>
<%= player.local.nickname + ', ' + player.local.race + ', ' + player.local.league%>, <a href="/profile-details/<%= player._id%>">Profil jucator</a>
</li>
<%});%>
However, i get undefined for the avatar.imagePath and avatar.imageName, everything else works fine. Is there a better way to do this? What am i missing/doing wrong? Thank you in advance!
"you can call ‘populate’ to easily resolve an ObjectID into the associated object, but you can’t call ‘populate’ to resolve an ObjectID that’s contained in that object. Furthermore, since the populated object is not technically a Document, you can’t call any functions you attached to the schema. Although this is definitely a severe limitation, it can often be avoided by the use of nested schemas"
Please refer to https://thecodebarbarian.wordpress.com/2013/06/06/61/ for alternatives to solve your issue
You are getting that issue because the path players.local.avatar is not getting populated. For sub-populate you need to loop through the tournament.players and populate the local field in there.