I got this mongoose schema
const UserSchema = new Schema({
email: {
type: String,
required: true,
unique: true,
},
groups: [
{
groupName: {
type: String,
required: true,
},
groupMembers: [{ type: Schema.Types.ObjectId, ref: "GroupMember" }],
},
],
});
I am trying to populate the groupMembers
const user = await User.findById(req.params.userId).populate({
path: "groups",
populate: {
path: "groupMembers",
},
});
When I am logging the 'user', the groupMembers
array is not populated, it's empty(even though I have objects inside).
Edit: found the solution- the problem was that when I added new GroupMember
I did'nt .save()
it.
CodePudding user response:
Try this:
const user = await User.findById(req.params.userId).populate('groups.groupMembers');