从 Firebase RTDB 查询特定的数据列表?

Query a specific list of data from Firebase RTDB?

有没有比这更好的方法来查询 Firebase RTDB 中的特定数据集?我能想到的就是使用 forEach() 并在每个发出的可观察对象上推送到订阅内部的 BehaviorSubject。让我知道!我已经坚持了一段时间。

hits = new BehaviorSubject([]);

territories = ['16830', '16832', '16838'] // Zip Codes

getTerritories(territories) {
    territories.forEach(key => {
      this.db.object(`locations/${key}`).snapshotChanges()
      .map(zip => zip.payload.val())
      .subscribe(zip => {
        let currentHits = this.hits.getValue();
        currentHits.push(zip);
        this.hits.next(currentHits);
      });
    });
   }

this.hits.subscribe(res => console.log(res));

您可以使用 Observable.forkJoin():

Observable.forkJoin(
    //this returns an array of observables.
    territories.map(key => this.db.object(`locations/${key}`)
        .map(zip => zip.payload.val()))
)
    .subscribe(res => console.log(res));

请注意,Observable.forkJoin 接受一组可观察对象,并并行触发它们,等待所有这些完成,然后再发出一个值。