-
-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bi-directional virtuals population issue #370
Comments
btw, if I populate the Followshops from users, like this public static getUserWithFollowers(this: ReturnModelType<typeof User>,
id: string
): Promise<DocumentType<User> | null> {
return this.findById(id)
.populate({path: 'followers', select: 'followerId', populate: 'follower'})
.exec();
} the
as you can see, again it tries to lookup the |
@grreeenn firstly, when you set secondly, i dont know if this is just the example, but in
this isnt much different, but i know them in terms like |
Thanks for your reply.
Ok, so I removed the types from
which didn't influence the behavior.
That's something new to me. Should I populate the path of the foreign model? Path |
Just to clarify: @modelOptions({
schemaOptions: {
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
})
export class User {
@prop({ required: true })
public name!: string;
@prop({unique: true})
public email!: string;
@prop()
public password!: string;
// @prop({
// ref: Followship,
// localField: "_id",
// foreignField: "followeeId",
// justOne: false
// })
// public followers?: Ref<Followship>[];
//
// @prop({
// ref: Followship,
// localField: "_id",
// foreignField: "followerId",
// justOne: false
// })
// public followees?: Ref<Followship>[];
public static getById(this: ReturnModelType<typeof User>,
id: string
): Promise<DocumentType<User> | null> {
return this.findById(id)
.select('name email address')
.populate('followees')
.populate('followers')
.exec();
}
} the
|
sorry my bad, i read it wrong sorry that i read your issue wrong, if you use virtuals with |
the issue I'm solving with that is typing - when using virtuals, I get related object IDs in an array of IDs, and if I need objects themselves - I get them as an array of objects in a different key. This way, for example, I always know that the |
Marking Issue as stale, will be closed in 7 days if no more activity is seen |
Closing Issue because it is marked as stale |
Versions
Code Example
Thank you for your work - Typegoose makes life with Mongo and Typescript a breeze :)
I have a strange issue (probably, because I'm trying to do a strange thing).
In my use case, Users can follow each other, and these relations contain additional properties - so I moved them to the separate model Followship.
Followship has
followerId
andfolloweeId
(both IDs of Users) to define the relation. What I want is an ability to pull the Followships separately and populate Users in them, and also an ability to pull Users with their Followships populated.So here is how my models look:
User:
Followship:
The issue is the following:
When I call
User.getById()
, I get the User object correctly populated with followers and followees (is there a better term for it in English btw?). But when I callFollowship.getFollowersOfUser()
, I get the array of Followships - withfollower: null
.Debug mode shows, that
Followship.getFollowersOfUser()
tries to find the User ids in the Followship collection, and obviously fails:The thing is, that if I remove the virtual fields from User model,
Followship.getFollowersOfUser()
returns just fine:Am I doing something wrong? Is there any workaround for this? Except using only the User's virtuals
I've seen the szokodiakos/typegoose#295, but I'm not sure I understand the examples there.
Do you know why it happens?
no
The text was updated successfully, but these errors were encountered: