代码之家  ›  专栏  ›  技术社区  ›  JoeKer

AngularFire2/firestore valueChanges()存在数据时返回null

  •  6
  • JoeKer  · 技术社区  · 6 年前

    我正在尝试在加载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 回复  |  直到 6 年前
        1
  •  1
  •   JoeKer    6 年前

    如果没有返回数据,我们就会抛出一个错误,然后重试。

    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);