【问题标题】:Expert wanted AngularFire2 firestore nested collection get data?专家想要 AngularFire2 firestore 嵌套集合获取数据?
【发布时间】:2018-10-26 19:26:18
【问题描述】:

嘿,我找不到如何在 firestore 中进行嵌套查询?

this.match1 = this.MatchCollection1.snapshotChanges().pipe(map(actions => {
    return actions.map(a => {
        const data = a.payload.doc.data() as Matches;
        const uid = data.pairer;
        const id = a.payload.doc.id;
        const photourl = afs.collection(`users/${uid}/photos`,
            ref => ref.where('index', '==', 0).where('deleted', '==', false)).snapshotChanges().pipe(map(action => {
            return action.map(p => {
                const pdata = p.payload.doc.data() as Photos;
                return pdata.url;
            });
        }));
        return {id, uid, url: photourl, ...data};
    });
}));

我的问题 photourl 返回 observable 我怎样才能像其他人一样返回对象

【问题讨论】:

  • 谁知道这个?
  • 你能提供你的数据结构并解释你期望什么样的结果吗?
  • 我想把 photourl 串起来

标签: firebase nested angular6 angularfire2 rxjs6


【解决方案1】:

有点棘手,但我认为您需要做的就是移动您的 return {id, uid, url: photourl, ...data};从我所见,您只是缺少一个订阅来解压您的可观察对象并提取价值。 EG

 return action.map(p => {
              const pdata = p.payload.doc.data() as Photos;
              return pdata.url;
            });
          })).subscribe(photo => {
"if you want to set photourl do it here photourl = photo.url;"
            return photo; //do what you need to before you return the actual 
photo object.

          });

【讨论】:

    【解决方案2】:

    您可以将 toPromise 方法与 async await 一起使用。

    数据结构

    users
      | userId1
          | photos
              | photoId1
                    | url : 'someURL'
              | photoId2
                    | url : 'someURL2'
    

    服务或组件文件

    items;
    getPhotos(uid){
      return this.db.collection(`users/${uid}/photos`).valueChanges()
      .pipe(first()).toPromise() // get first stream and return it as promise
    }
    
    async fooFunc() {
    
      let user_id = 'userId1' //TODO: get user uid from somewhere
    
      this.items = await this.getPhotos(user_id) // await getPhotos promise finish
      console.log(this.items) // return [Object, Object]
    }
    

    组件html模板

    <li *ngFor="let item of items">
        {{ item | json }}
    </li>    
    

    输出

    { "url": "someURL" }
    { "url": "someURL2" }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2020-09-09
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2020-09-03
      相关资源
      最近更新 更多