Skip to content Skip to sidebar Skip to footer

How To Fetch Data From Firestore And Inside It Fetch Another Data (by Reference)

I can't figure out how to fetch data from firestore when i have in data model reference (id) to another object, for example like this City {name: string; countryId: st

Solution 1:

So i manage to solve this problem at the end. Here is code from servis.

 getCity(ref: string): Observable<City> {
    return this.dataFetch.getDocument(ref)
      .switchMap((res: City) => {
        return this.dataFetch.getDocument(res.countryId)
          .map((country: Country) => {
            return new City(
              res.id,
              res.name,
              country.name
            );
          });
      });
  }

Then you can subscribe to this observable in your component or use async pipe. Also, I found usefull link where is described how to use reference and geo type in FireStore.


Post a Comment for "How To Fetch Data From Firestore And Inside It Fetch Another Data (by Reference)"