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.

  • 相关阅读:
    python基础12-语法
    基础篇-内置函数(常用)
    中级篇-内置函数 (map/filter/reduce)
    python 基础11-递归
    python 基础10-函数、变量
    python 基础9-拼接
    redis
    python--os模块
    函数return多个值
    python--文件读写
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7689626.html
Copyright © 2011-2022 走看看