当有数据时,AngularFire2 / firestore valueChanges()返回null

时间:2018-02-15 16:33:14

标签: google-cloud-firestore angularfire2

我正在尝试在加载angularfire2应用程序时从firestore集合中获取文档。在隐身模式下加载应用程序时,该函数在第一次加载时返回null,但在刷新后它返回我期望的数据。

public GetConfig(): Observable<Config> {
return this.documentDB
  .collection("Configs")
  .valueChanges()
  .do(c => {
    this.SetCurrentVersion((c[0] as Config).currentversion);
  })
  .map(c => c[0] as Config);

}

有没有其他人遇到这样的问题?我已经确认Configs集合中有可以退回的文档。我的angularfire2版本是5.0.0-rc.4。

我也尝试过使用snapshotChanges并从集合中获取特定文档,所有在首次加载时都为null并且在刷新时工作。

1 个答案:

答案 0 :(得分:0)

如果它没有返回数据然后重试,我们最终会抛出错误。

return this.documentDB
    .collection("Configs")
    .valueChanges()
    .do(c => {
      if (c.length > 0 && c != null) {
        this.config = c[0] as Config;
        this.SetCurrentVersion(this.config.currentversion);
      }
    })
    .map(c => {
      if (c.length === 0) {
        throw new Error("could not get config");
      } else {
        return c[0] as Config;
      }}).retry(5);