过滤文档参考作为字段

时间:2018-10-22 10:12:20

标签: angular google-cloud-firestore angularfire2

我有3个收藏集:

  1. User =>带有某些字段
  2. 组织=>带有某些字段
  3. UserOrg,其字段如下:
    • uid //用户ID
    • orgid // orgid
    • userDetails //用户文档的文档引用,例如user / uid
    • orgDetails //像org / uid这样的org doc的文档参考

现在我需要获取用户详细信息的用户详细信息

我的代码是:

return this.dbCollectionService.userOrganizationCollection.snapshotChanges()
      .pipe(map(res => {
        return res.map(data => {
          var userData = data.payload.doc.data()
         let userDetails : any;
         if (userData.userDetails) {

this.angularFirestore.doc(userData.userDetails).valueChanges().subscribe(res => {
          userDetails = res;
          console.log(userDetails);
        });

      return { id, ...userDetails }

    }).filter( f => f.organizationId != undefined && f.organizationId == orgId)
  }));

这能够正确控制结果,但不会返回数据。

可能需要花费一些时间从数据引用中获取数据,并且return语句会更早执行。

1 个答案:

答案 0 :(得分:0)

return { id, ...userDetails }上进行预订之前,将执行this.angularFirestore .doc(userData.userDetails).valueChanges()

订阅内的订阅不是一个好主意,您应该使用其他可插拔的运算符(如switchMap)来链接两个调用:

return this.dbCollectionService
.userOrganizationCollection.snapshotChanges()
.pipe(
   map(res => res.map(data => data.payload.doc.data())),
   map(userDatas => userDatas.filter(_ => _)),
   map(userDatas => userDatas.map(this.angularFirestore.doc(userData.userDetails).valueChanges()
                                    .pipe(map(userDetails => ({ id, ...userDetails }))))),
   switchMap(userDatas$ => combineLatest(userDatas$)),
   map(users => users.filter( f => f.organizationId != undefined && f.organizationId == orgId))
);