Reputation: 3841
My user model:
export const UserInfoSchema = new Schema<IUserInfo>({
name: {
type: String,
},
associatedTeams: [
{
type: ObjectId, // Schema.Types.ObjectId
ref: "Team",
},
],
});
and my Team model:
const TeamSchema = new Schema<ITeam>(
{
teamName: {
type: String,
required: true,
},
admins: [
{
type: ObjectId,
ref: "UserInfo",
},
],
}
);
export default models.Team || model<ITeam>("Team", TeamSchema);
If I save a new Team and place the ObjectId in associatedTeams on the UserInfo model, I should be able to do:
if (user) { // user is a document of UserInfo model
await user.populate("associatedTeams");
}
However, this is not working, and the ObjectIds are not being populated. I've spent several hours trying to work out why - if anyone could help that would be appreciated.
Upvotes: 0
Views: 28
Reputation: 650
When you are trying to apply populate on doc which is already in memory, you should use execPopulate
await user.populate("associatedTeams").execPopulate()
Upvotes: 1