简体   繁体   English

AngularFire - Firestore - 如何获取子集合并合并到HTML中

[英]AngularFire - Firestore - How to get sub collection and merge into HTML

I have a scenario at the moment where I have a collection of issues which i successfully fetch. 我现在有一个场景,我有成功获取的问题集合。 In firestore, each issue ( issues > issueID ) has a sub-collection called images . 在firestore中,每个问题( issues > issueID )都有一个名为images的子集合。

In that collection there are doc id's per image. 在该集合中,每个图像都有doc id。 ( issues > issueID > images > imageID ). issues > issueID > images > imageID )。 I have a page that's displaying all issues, and I'd like to show all related image per issue also. 我有一个显示所有问题的页面,我也希望每个问题都显示所有相关图像。

In my HTML below i have added a comment of where i would like the issue images to go. 在我的HTML下面,我添加了一个评论,我希望问题图像去哪里。

Desired result: 期望的结果:

  • Issue 1 问题1
    • Issue 1 images 问题1图像
  • Issue 2 问题2
    • Issue 2 images 问题2图像

etc 等等

Get Issues 得到问题

 this.issuesCollection = this.afs.collection<any>(`users/${this.userID}/projects/${this.project_id}/issues`, ref => ref.orderBy('issue_order'));
      // this.issues = this.issuesCollection.valueChanges();   
      this.issues = this.issuesCollection.snapshotChanges().pipe(
        map(actions => actions.map(a => {
          const data = a.payload.doc.data();
          const id = a.payload.doc.id;
          return { id, ...data };
        }))
      );

HTML HTML

<ul>
   <li *ngFor="let issue of issues | async">
      {{issue.issue_title}}
      <ul>
         <li>
             <!-- THIS IS WHERE I WOULD LIKE THE IMAGES PER ISSUE TO GO -->      
         </li>
      </ul>
    </li>
</ul>

Images Collection Reference This is the location of the images, where id would be the ID of the issue. 图像集合参考这是图像的位置,其中id是问题的ID。 This hasn't been hooked up, but is where the location of the images would be. 这并没有被连接起来,而是图像的位置。

this.imagesCollection = this.afs.collection<any>(`users/${this.userID}/projects/${this.project_id}/issues/${id}/images`);
this.images = this.imagesCollection.snapshotChanges()

There is new feature in firebase called Collection group query , which definitely can help in your case. firebase中有一个名为Collection group query的新功能,在你的情况下肯定会有所帮助。 But unfortunately i dont think this has been already implemented in angularfire2. 但不幸的是,我不认为这已经在angularfire2中实现了。

You can use RxJS MergeMap 您可以使用RxJS MergeMap

  const collection = this.firestore.collection('users');
  return  collection.valueChanges.pipe(
    mergeMap( (users: User[]) =>  
               this.firestore.doc(`stories/${users[0].uid}`).
                        valueChanges().pipe(map(
                          (storie) => Object.assign({}, {users[0].uid, ...users[0], ...stories}
                      ))
           ))
   );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM