简体   繁体   English

如何从Firestore中获取数据以及如何在其中存储其他数据(通过引用)

[英]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 当我在数据模型引用(id)中引用另一个对象时,例如我这样,我不知道如何从firestore中获取数据

City {name:      string;
       countryId: string; //primary key to another object in database
}
Country {
      name: string;
}

I m using AngularFire 5. 我正在使用AngularFire 5。

After i fetch city i want fetch country and i want to asign country.name to city.countryId and i want return joined object city. 在我获取城市之后,我想要获取国家,并且我想要将country.name分配给city.countryId,并且我希望返回加入的对象城市。
I made service for this because i want fetch this data from multiple places in code. 我为此提供了服务,因为我想从代码的多个位置获取此数据。

@Injectable()
export class CityService implements  OnInit {
  city: City;

  constructor(
    private dataFetch: FireStoreService) { }
  ngOnInit() {}

  getCity(ref: string): City {
    this.dataFetch.getDataDoc(ref).subscribe((_city: City) => {
      this.dataFetch.getDataDoc(_city.countryId)
        .subscribe((country: Country) => {
          _city.countryId = country.name;
          this.city = _city;
        });
    });
    return this.city;
  }
}

Ye i know this will not work beacause it is async task, i have read a lot of articles, but i can not still figure out. 是的,我知道这是行不通的,因为它是异步任务,我已经阅读了很多文章,但是我仍然无法弄清楚。 So I don't know how to fetch some object and then fetch references from this object and return joined object (without references but with proper data). 因此,我不知道如何获取某个对象,然后从该对象获取引用并返回联接的对象(没有引用但有适当的数据)。
This is my city component. 这是我的城市组成部分。

 @Component({
      selector: 'app-detail-city',
      template: `
        <p> detail-city works! </p>
        <p> Name : {{ city.name }} </p>
        <p> Country : {{ city.countryId }} </p>
        <p> ID : {{ city.id }} </p>
      `,
      styleUrls: ['./detail-city.component.css']
    })
    export class DetailCityComponent implements OnInit {
      city: City;
      root: string;

      constructor(
        private route: ActivatedRoute,
        private cityService: CityService) {
        this.route.params.subscribe(
          (params: Params) => {
            this.root = params['root']+'/'+params['id'];

            this.city = cityService.getCity(this.root);

          });
      }
      ngOnInit() {}
    }

So i manage to solve this problem at the end. 因此,我最终解决了这个问题。 Here is code from servis. 这是来自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. 此外,我发现了有用的链接 ,其中描述了如何在FireStore中使用参考和地理类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM