Search code examples
javascriptmongodbtypescriptmongoosetypegoose

Typegoose find() on model does not return reference array fields


i am inserting data into mongodb with this Model:

export class Mapresult extends Typegoose {
    @prop()
    match_id: string;

    @prop()
    map: string;

    @prop({ ref: Team })
    winner?: Ref<Team>;

    @prop()
    demo_link: string;

    @prop({ ref: Team })
    public teams?: Ref<Team>[];

    @prop({ ref: Teamresult })
    public team_results?: Ref<Teamresult>[];
}
export const MapresultModel = new Mapresult().getModelForClass(Mapresult);

and am populating teams and team_results later.

In the mongodb webinterface, both prop arrays have values: screenshot of mongodb entry

But when i run

MapresultModel.findOne({ match_id: req.params.match_id, map: req.params.map_name })
    .then((result) => {
        return result.populate("winner").populate("teams").populate("team_results").execPopulate();
    })
    .then((result) => {
        res.send(result);
    });

the result i am receiving is:

{
    _id: 5f9843e2915f421a1866b822,
    match_id: '1-96be1535-8671-4790-80ac-2bbcf1f8dfa2',
    map: 'de_overpass',
    demo_link: 'https://demos-europe-west2.faceit-cdn.net/csgo/4bcc5d50-e5c6-4510-a0df-1430774482af.dem.gz',
    __v: 0,
    winner: {
        _id: 5f9843e2915f421a1866b81d,
        myid: '069f67d6-a217-4635-9618-a2e8882fc678',
        name: 'GoPott',
        team_logo: 'https://assets.faceit-cdn.net/teams_avatars/069f67d6-a217-4635-9618-a2e8882fc678_1581456059302.jpg',
        __v: 0
    }

}

And as you can see, winner is being populated, while teams and team_results are not in the result. Even before populating, these props are not in the result.

How do i get teams and team_results to be in the result?


Solution

  • Update the Typegoose version. I was using @hasezoey/typegoose which was on version 5.9. The right package is @typegoose/typegoose with version ^7.4.1. Fix all the errors after updating the typegoose version and it should work.