Angular 7和firestore,从另一个集合中获取与另一个集合中的每个文档相对应的数据

时间:2019-02-07 14:17:56

标签: angular join collections google-cloud-firestore angularfire2

我要做的工作列出了属于每个客户的设备。

我已经阅读,观看了视频,复制和粘贴的结果,但是没有任何效果,而且我的头在旋转。

我需要每个客户和设备的ID,所以我要像这样

fetchJobs() {
  this.todoCollectionRef = this.afs.collection('clients');
  this.todo$ = this.todoCollectionRef.snapshotChanges().pipe(map(actions => {
    return actions.map(action => {
      const data = action.payload.doc.data() as Todo;
      const id = action.payload.doc.id;
      return {
        id,
        data
      };
    });
  }));

}


fetchEquipment() {
  this.equipmentCollectionRef = this.afs.collection('equipment');
  this.equipment$ = this.equipmentCollectionRef.snapshotChanges().pipe(map(actions => {
    return actions.map(action => {
      const data = action.payload.doc.data() as Equipment;
      const id = action.payload.doc.id;
      return {
        id,
        data
      };
    });
  }));

}

我还尝试过重新考虑我的数据结构,但是由于每个客户端将具有多个项目,而数量却不尽相同,因此我尝试了带有映射的数组,但无法更新详细信息。

如果有人可以指出正确的方向,我将不胜感激。

Screenshot

我认为我需要这样的东西。

  this.todoCollectionRef = this.afs.collection('clients');
    this.todo$ = this.todoCollectionRef.snapshotChanges().pipe(map(actions => {
      return actions.map(action => {
        const data = action.payload.doc.data() as Todo;
        const id = action.payload.doc.id;
        return this.afs.collection('equipment', ref => ref.where('client', '==', data.client)).valueChanges()
        .pipe(map(eqip => Object.assign({}, {id, data, eqip})));
    });

})).switchMap(observables => Observable.combineLatest(observables));

      }  
If anyone can help here I would be truly grateful.
And if someone could explain the code to help me and others learn.   

1 个答案:

答案 0 :(得分:0)

使用fetchEquipement功能,您正在使用“客户端”的集合,则应使用 this.equipmentCollectionRef