Angularfire2 将更多数据附加到用户集合 [英] Angularfire2 attach more data to user collection

查看:16
本文介绍了Angularfire2 将更多数据附加到用户集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,用户可以在其中创建组.组集合存储在每个用户内部,如果组的所有者广告成员对应的用户 id 和角色只保存在组集合中.因此,当所有者访问群组成员时,我希望显示其他群组中的用户的完整信息.

I am creating an app where users can create groups. group collection is stored inside each user, and if owner of the group ads members corresponding users id and role only saved inside group collection. So when owner visits group members i wish to show full info of users which is in another collection out side groups.

这是我的 firestore 数据库中的两个集合

these are two collections in my firestore database

this.afs.collection(`users/${ownerId}/groups/${gid}/users`)
this.afs.doc(`users/${ownerId}`) //full user data

这是我在我的服务中访问用户的方式

here is how i access users in my services

getGroupUsers(gid, ownerId) {
return this.afs.collection(`users/${ownerId}/groups/${gid}/users`).snapshotChanges().pipe(
    map(actions => actions.map(a => {
      const data = a.payload.doc.data();
      const userId = a.payload.doc.id;
      return { userId, ...data };
    })
    )
  );
}

这里 userId 是每个用户的唯一 ID,我希望向 ...data 对象添加更多数据.我尝试了很多方法都失败了,这就是我尝试实现的

here userId is each users unique id and i wish to add more data to ...data object. I tried many ways and failed, here is what i try to achive

getGroupUsers(gid, ownerId) {
return this.afs.collection(`users/${ownerId}/groups/${gid}/users`).snapshotChanges().pipe(
  map(actions => actions.map(a => {
    const data = a.payload.doc.data();
    const userId = a.payload.doc.id;
    //return { userId, ...data };
    return this.afs.doc(`users/${userId}`).valueChanges().pipe(
      map(userData => {
        return {userId, ...data, ...userData}
      })
    )
  })
  )
);
}

我使用 angular 8.1.2Rxjs 6.4.0.请帮忙.

I use angular 8.1.2 and Rxjs 6.4.0. Please help.

推荐答案

未测试,但可以组合使用 mergeMapswitchMapcombineLatest.switchMap 切换到内部 observable 和 combineLatest 执行嵌套请求.

Not tested, but you can use a combination of mergeMap or switchMap and combineLatest. switchMap to switch to the inner observable and combineLatest to perform the nested requests.

getGroupUsers(gid, ownerId) {
  return this.afs.collection(`users/${ownerId}/groups/${gid}/users`).snapshotChanges().pipe(
    switchMap(actions => combineLatest(
      actions.map(a => {
        const data = a.payload.doc.data();
        const userId = a.payload.doc.id;
        return this.afs.doc(`users/${userId}`).valueChanges().pipe(
          map(userData => {
            return { userId, ...data, ...userData }
          })
        )
      })
    ))
  );
}

这篇关于Angularfire2 将更多数据附加到用户集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆