【问题标题】:Angular 7 and firestore, get data from another collection that corresponds to each document on another collectionAngular 7和firestore,从另一个集合中获取与另一个集合上的每个文档相对应的数据
【发布时间】:2019-07-01 16:34:31
【问题描述】:

我要做什么列出属于每个客户的设备。

我已阅读、观看视频、复制和粘贴结果,但没有任何效果,我的头晕目眩。

我需要每个客户端和设备的 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
      };
    });
  }));

}

我也尝试重新考虑我的数据结构,但由于每个客户端都会有多个项目并且数量不一样,所以我尝试使用带有地图的数组但无法更新详细信息。

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

我想我需要这样的东西..

  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.   

【问题讨论】:

  • 你真正想要做什么?明确你的观点
  • 我要做的是列出客户(已完成)然后查询属于该客户的设备的第二个集合并在同一页面上列出每个设备,所以我想我会通过客户名称查询,所以我的collection1.doc 将有名称、地址、电话号码等,然后collection2.doc 将有名称、品牌、型号、序列等。
  • 我会在电脑前截屏,希望它能提供一个明确的目标。
  • 尝试使用客户端 ID 存储设备,当您想要查询设备时,使用 .where() 函数根据客户端 ID 检索数据。如果您不知道如何使用 .where() 进行查询,我会为您发布答案。
  • 我一直在尝试的是 asf.collection('equipment', ref => ref.where('clientname', '==' this.name));我正在做的是将客户端名称从客户端集合传递到设备一。这就是我被卡住的地方。如果我使用按钮将名称传递给设备,我可以做到这一点,但我正在寻找一种不通过按钮传递名称的方法。

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


【解决方案1】:

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

【讨论】:

  • 这是我创建帖子时的拼写错误,已修改我的帖子。谢谢
  • 我无法列出属于每个客户的设备,我如何获取列出的该客户的详细信息以及其他详细信息?我需要能够获得设备的 ID。
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多