专家想要AngularFire2 firestore嵌套集合获取数据吗?

时间:2018-10-26 19:26:18

标签: firebase nested angular6 angularfire2 rxjs6

嘿,我找不到如何在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返回可观察到,我该如何像其他对象一样返回对象

2 个答案:

答案 0 :(得分:0)

要执行一些棘手的操作,但我认为您需要做的就是移动您的返回{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.

          });

答案 1 :(得分:0)

您可以在 async await 中使用 toPromise 方法。

数据结构

if (roleid.equals("1")) {
    Intent intent = new Intent(MainActivity.this, Dashboard.class);
    startActivity(intent);
} else { 
    Toast.makeText(MainActivity.this, cvalue, Toast.LENGTH_SHORT).show();
}

服务或组件文件

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

组件html模板

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]
}

输出

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