AngularFire2如何在列表中查询列表

时间:2019-02-14 22:49:58

标签: javascript firebase google-cloud-firestore angularfire2 ionic4

我正在尝试构建一个队列系统,其中有一个我从一个Firestore查询中获得的“站点”列表,并且在该列表中的每个项中,我要显示该站点当前的“项目”。最终结果如下所示:

station 1
 - project 1, project 3, project 5

station 2
 - project 2, project 6

station 3
 - project 4, project 7, project 8

etc.

作为参考,我的HTML如下所示:

    <ul>
      <li *ngFor="let shop of shops | async">
        {{ shop.StationtName}}
             <div *ngFor="let shopProject of shopProjects | async">
                 {{ shopProject.SKU}}
             </div>
      </li>
    </ul>

我解决此问题的方法如下:

this.shopCollection = this.afs.collection('shop', ref => ref.where('station', '==', 'true'))

    this.shops = this.shopCollection.snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data() as Shop;
        const id = a.payload.doc.id;
        console.log(data.StationName);

<///////////// I'm trying to use data.StationName in the next query ///////////>

                    this.projectCollection = this.afs.collection('shop', ref => ref.where('stationLocation', '==', data.StationtName)) 
                    this.shopProjects = this.projectCollection.snapshotChanges().pipe(    
                      map(actions => actions.map(a => {
                        const data = a.payload.doc.data() as Prod;
                        const id = a.payload.doc.id;
                        console.log(data.SKU);
                        return { id, ...data };
                      }))
                    );

        return { id, ...data };
      }))

    );

结果是在每个记录下具有相同项目SKU的电台列表。

赞:

station 1
 - project 8
station 2
 - project 8
station 3 
- project 8
station 4 
- project 8

我不确定我是否以最有效的方式来解决这个问题,任何输入都会被很好地接受。预先感谢

0 个答案:

没有答案