【问题标题】:Query a specific list of data from Firebase RTDB?从 Firebase RTDB 查询特定的数据列表?
【发布时间】:2018-04-24 16:42:14
【问题描述】:

有没有比这更好的方法来查询 Firebase RTDB 中的特定数据集?我能想到的就是使用 forEach() 并在每个发出的 observable 上推送到订阅内的 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));

【问题讨论】:

    标签: angular firebase firebase-realtime-database rxjs behaviorsubject


    【解决方案1】:

    你可以使用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 接收一组可观察对象,并并行触发它们,等待它们全部完成,然后再发出一个值。

    【讨论】:

    • 我能够通过 combineLatest 实现我想要的。感谢您指出我正确的方向!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    相关资源
    最近更新 更多