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

查看:45
本文介绍了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. 请帮忙.

推荐答案

未经测试,但可以结合使用 mergeMap switchMap combineLatest . switchMap切换到内部可观察的,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天全站免登陆