具有多个Firestore分支的RxJs流

时间:2019-09-30 12:43:01

标签: angular google-cloud-firestore rxjs rxjs-observables

我有一个规范化的Firestore结构,其中包含多个集合,但是正在努力编写一个函数以向我返回具有多个查找和数据调用的数据集。我具有以下FS结构

--friends // collection - (array of strings)
   --uasfm923je2j3j3j (user Id - document)
    --friends (array of strings)
      -- ijdu0923jfe0w9wee (userId of friends)
      -- 9rie3i3i3oo3i3i3

--users // collection
  --9034rjf90jwo230 (userId - document)
    -- name, dob, address (fields)

--meetingsVsUsers // collection
  --9034rjf90jwo230 (userId - document)
    --allMeetings (array of strings)
      -- 939i3828u2ik2k2k (meeting Id)

--meetingDetails
  --939i3828u2ik2k2k (meeting Id - document)
   -- date, time, place etc (fields)

我需要观察的是我所有的朋友会议,并附有他们的用户信息,即

会议:{日期:“ 1月1日”,时间:“ 12:00”,地点:“房屋”,所有者信息:{名称:“乔”,多布“ 1970年1月1日”})

我找不到RxJs操作的正确组合来实现这一目标。我需要

  1. 返回当前用户朋友的列表(朋友收藏)
  2. 获取与该用户相关联的所有MeetingId(usersVsMeetings集合)
  3. 获取每个会议的所有详细信息(会议集合)
  4. 获取用户详细信息并附加到每个会议(用户集合)

这是我到目前为止的代码,它返回一个数组,正确的用户配置文件为[0],会议详细信息在[1]中,但仅在用户只有一个会议的情况下有效

return this.firestoreSvc.documentAsObservable(`friends/${this.loggedOnUserProfile.uid}`).pipe(
        switchMap(friends => from(friends['friends'])),
        mergeMap(friendId => {
            const meetingsWithProfile = combineLatest(
                this.firestoreSvc.documentAsObservable(`users/${friendId}`),
                this.firestoreSvc.documentAsObservable(`usersVsMeetings/${friendId}`)
                .pipe(switchMap(allMeetings => this.firestoreSvc.documentAsObservable(`meetings/${allMeetings['allMeetings']}`)))
            )
            .pipe(map(data => ({ friendProfile: data[0], meeting: data[1] })))
            meetingsWithProfiles$.push(meetingWithProfile);
            return meetingsWithProfiles$.length ? combineLatest(meetingsWithProfiles$) : of([]);
        })
    );

我也尝试过forkJoin和concat,与switchMap相对,但我无法弄清楚我需要切换到哪个可观察对象并取消(即,当您获得会议ID时)以及在需要数据时保留哪些可观察对象(即完整的会议详细信息)

有人愿意提供帮助

谢谢

0 个答案:

没有答案