Here are two simplified entities that I am working with:
User.ts
@Entity({ collection: "users" })
export class User {
@PrimaryKey()
_id!: ObjectId;
@SerializedPrimaryKey()
id!: string;
/** The user's lead class */
@OneToOne({ inversedBy: "classmaster", orphanRemoval: false })
lead_class?: Reference<Class>;
}
Class.ts
@Entity({ collection: "classes" })
export class Class {
@PrimaryKey()
_id!: ObjectId;
@SerializedPrimaryKey()
id!: string;
/**
* Classmaster
* - 'null' if no assigned classmaster.
*/
@OneToOne({ mappedBy: "lead_class" })
classmaster?: Reference<User>;
}
Although I can access the class reference from the User side, I'm having trouble accessing the classmaster (user reference) from the Class side. Even after trying to populate, I have been unsuccessful. How can I retrieve the user from the inverse side? Is this not achievable? Below is a code snippet for reference:
let c = await db.classes.findOneOrFail(
{ id: "example_id" },
{ strict: true, populate: ["classmaster"] }
);
console.log("Classmaster: ", c.classmaster); // displays as undefined