Angularfire2和Firestore - 检索集合列表的所有子集合内容

问题描述 投票:0回答:1

我尝试根据第一次调用时收到的密钥在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个回答
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);

如果你要经常使用这个功能,你应该开始使用qazxsw poi。

© www.soinside.com 2019 - 2024. All rights reserved.