如何从Firestore中获取数据以及如何在其中存储其他数据(通过引用) [英] How to fetch data from firestore and inside it fetch another data (by reference)

查看:54
本文介绍了如何从Firestore中获取数据以及如何在其中存储其他数据(通过引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在数据模型引用(id)中引用另一个对象(例如这样的

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: string; //primary key to another object in database
}
Country {
      name: string;
}

我正在使用AngularFire 5.

I m using AngularFire 5.

获取城市后,我想获取国家,并想将country.name分配给city.countryId,然后返回加入的对象city.
我为此提供了服务,因为我想从代码的多个位置获取此数据.

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.
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() {}
    }

推荐答案

所以我最终设法解决了这个问题.这是来自Servis的代码.

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
            );
          });
      });
  }

然后,您可以在组件中订阅此可观察项,也可以使用异步管道.另外,我发现了有用的链接,其中描述了如何使用参考和地理在FireStore中输入.

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中获取数据以及如何在其中存储其他数据(通过引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆