zoukankan      html  css  js  c++  java
  • [AngularFire] Resolve snapshotChanges doesn't emit value when data is empty

    Updated to AngularFire2 v5.0. 

    One important change is that you need to call .snapshotChanges() or .valueChanges() to get data as Observable back.

        return this.db.list<Skill>(`skills/${this.uid}`)
          .snapshotChanges()

    The problem is when there is no data, .snapshotChanges() never emit a value.

    Luckly it is Observable, we can solve the problem by using .startWith():

      getSkills(): Observable<Skill[]> {
        return this.db.list<Skill>(`skills/${this.uid}`)
          .snapshotChanges()
          .startWith([])
          .map((actions) =>
            actions.map((action) => ({$key: action.key, ...action.payload.val()}))
          );
      }

    If there is no data coming back from firebase, we still emit a empty array. This is much friendly if you work with Ngrx. Because if you depends on .snapshotChange() to emit a value back to switch map to next action, it might just block there, and your next action will never get dispatched.

  • 相关阅读:
    KMP算法
    找出第二大的数
    webpack 3 优化
    CocoaPods安装
    自适应水平垂直居中
    找出两个数组中都有,并且重复次数最多的元素
    swift 笔记
    Promise 用es5的基础实现
    $.ajax仿axios封装
    js基础拖拽效果
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7689626.html
Copyright © 2011-2022 走看看