【问题标题】:How to make firebase data read only once in typescript如何使firebase数据在打字稿中只读一次
【发布时间】:2018-10-03 22:33:33
【问题描述】:
readProductFromServer() {

    this.qS = this.afDatabase.list('product_table', ref => {

      return ref.limitToLast(1000) ;


    }).snapshotChanges().map(changes => {
      return changes.map(c => ({ key1: c.payload.key, ...c.payload.val() }));
    });
    this.qS.subscribe(values => {

    });
  }

如何使这段代码只读一次。我在互联网上搜索并没有得到我的代码的答案,StackOverflow 中提供了一些解决方案但与我的代码不匹配?

【问题讨论】:

    标签: angular typescript firebase firebase-realtime-database


    【解决方案1】:

    由于它是可观察的,如果您只想读取一次数据并且不想获得任何可能更新的新排放,只需在您的运营商链中添加take(1)

    this.qS = this.afDatabase.list('product_table', ref => {
        return ref.limitToLast(1000) ;
    }).snapshotChanges().map(changes => {
        return changes.map(c => ({ key1: c.payload.key, ...c.payload.val() }));
    }).take(1);
    
    this.qS.subscribe(values => {
    
    });
    

    它将首先发射,然后完成可观察。

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 2020-08-25
      • 2018-08-21
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多