在嵌套的 snapshotchanges 地图上返回可观察的元数据而不是预期的对象

Returning Observable Metadata Instead of Expected Objects on nested snapshotchanges maps

我正在尝试将用户 ID 解析为显示名称并将其添加到服务 return。它似乎正在通过 console.log 工作,但调用 TS 文件中的实际 return return 是可观察到的废话。

服务 1

getEventList(pid: string, sid: string) {

return this.afs.collection(this.partyCollectionPath + pid + this.sessionCollectionPath + sid + this.eventCollectionPath, ref => ref.orderBy('timestamp', 'desc')).snapshotChanges().pipe(
  map(actions => {
    return actions.map(a => {
      const data = a.payload.doc.data({ serverTimestamps: 'estimate' }) as events_list;
      const id = a.payload.doc.id;

      return this.authservice.getDisplayName(data.uid).subscribe((doc) => {
        const user = doc.payload.data() as User
        console.log('data', { id, ...data, user: { ...user } })
        return { id, ...data, user: { ...user } };
      })
    })}
  ))
}

服务 2

 getDisplayName(uid: string){
    const userRef: AngularFirestoreDocument<any> = this.afs.doc("users/"+uid)
    return userRef.snapshotChanges()
  }

调用TS文件

this.eventsService.getEventList(this.pid, this.sid).subscribe((eventData) => {
  this.eventData = eventData
  console.log('eventData', eventData);
})

服务 1 中的 console.log 向我显示了我想要 return 的确切对象,但是调用它的实际 TS 文件为我提供了一个看起来像这样的对象数组,而不是实际对象。

closed: false
destination: Ne {partialObserver: {…}}
initialTeardown: undefined
isStopped: false
_finalizers: [k]
_parentage: null

如果我 运行 这样的函数,它会完美运行。我猜这与我在第二个文档上使用订阅而不是 snapshotChanges 有关,混合订阅和快照可能不起作用我只是停留在如何前进上。

  getEventList(pid: string, sid: string) {

    return this.afs.collection(this.partyCollectionPath + pid + this.sessionCollectionPath + sid + this.eventCollectionPath, ref => ref.orderBy('timestamp', 'desc')).snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data({ serverTimestamps: 'estimate' }) as events_list;
          const id = a.payload.doc.id;
return { id, ...data}
          // return this.authservice.getDisplayName(data.uid).subscribe((doc) => {
          //   const user = doc.payload.data() as User
          //   console.log('data', { id, ...data, user: { ...user } })
          //   return { id, ...data, user: { ...user } };
          // })
        })}
      ))
  }

这根本不起作用,它甚至没有到达第二张地图中的 console.log。

 getEventList(pid: string, sid: string) {

    return this.afs.collection(this.partyCollectionPath + pid + this.sessionCollectionPath + sid + this.eventCollectionPath, ref => ref.orderBy('timestamp', 'desc')).snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data({ serverTimestamps: 'estimate' }) as events_list;
          const id = a.payload.doc.id;
          return this.afs.doc("users/" + data.uid).snapshotChanges().pipe(
            map(userData => {
              let user = userData.payload.data() as User
              console.log('data', { id, ...data, user: { ...user } })
              return { id, ...data, user: { ...user } };
            })
          )
        })
      }
      ))
  }

我非常接近,只需要在 2 个 snapshotChanges 上正确使用 mergemap 来获得我需要的东西:

  getEventList(pid: string, sid: string) {

    return this.afs.collection(this.partyCollectionPath + pid + this.sessionCollectionPath + sid + this.eventCollectionPath,
      ref => ref.orderBy('timestamp', 'desc')).snapshotChanges().pipe(
        map(actions => {
          return actions.map(a => {
            const data = a.payload.doc.data({ serverTimestamps: 'estimate' }) as events_list;
            const id = a.payload.doc.id;

            return this.afs.doc("users/" + data.uid).snapshotChanges().pipe(map(a => {
             const useData = a.payload.data() as User
             return { id, ...data, user: { ...useData}}
            }))
          })

        }
        ),
        mergeMap(obs => combineLatest(obs))
      )
  }