【问题标题】:Angularfire2 & Firestore – retrieve all subcollection content for a collection listAngularfire2 & Firestore – 检索集合列表的所有子集合内容
【发布时间】:2017-12-27 21:10:57
【问题描述】:

我尝试根据第一次调用时收到的密钥检索subcollection 中的数据。 基本上,我想要一个我所有用户的列表,每个用户都有一个子集合。

我能够从第一个 Payload 中检索数据,但不能从下面的 pointRef 检索数据

实现这一目标的正确方法是什么?

getCurrentLeaderboard() {
    return this.afs.collection('users').snapshotChanges().map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data()
        const id = a.payload.doc.id;
        const pointRef: Observable<any> = this.afs.collection('users').doc(`${id}`).collection('game').valueChanges()

        const points = pointRef.map(arr => {
          const sumPoint = arr.map(v => v.value)
          return sumPoint.length ? sumPoint.reduce((total, val) => total + val) : ''
        })

        return { id, first_name: data.first_name, point:points };
      })
    })
  }

【问题讨论】:

    标签: angularfire2 google-cloud-firestore


    【解决方案1】:

    我尝试将我的代码放在评论中,但我认为将其格式化为答案会更好。

    首先您需要订阅您的pointRef,然后您可以像这样更改您的代码。

    getCurrentLeaderboard() {
        return this.afs.collection('users').snapshotChanges().map(actions => {
          return actions.map(a => {
            const data = a.payload.doc.data()
            const id = a.payload.doc.id;
            const pointRef: Observable<any> = this.afs.object(`users/${id}/game`).valueChanges() // <--- Here
    
            const pointsObserver = pointRef.subscribe(points => { //<--- And Here         
              return { id, first_name: data.first_name, point:points };
            })
        })
     }
     ....
     //Usage:
     getCurrentLeaderboard.subscribe(points => this.points = points);
    

    如果你要经常使用这个功能,你应该开始denormalize your data

    【讨论】:

    • 感谢您的意见@Makah!观点还是有问题。我可以用&lt;div *ngFor="let data of points | async"&gt; {{ (data.id)}}&lt;/div&gt; 显示idfirst_name 但{{data.point}} 只返回一个[object] 我尝试使其异步,使用json,但我在这里看不到任何东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    相关资源
    最近更新 更多