JoeKer
JoeKer

Reputation: 71

AngularFire2 / firestore valueChanges() returning null when there is data

I'm trying to get a document out of a firestore collection on load of a angularfire2 application. When loading the application in incognito mode the function returns null on first load, but after a refresh it returns the data I'm expecting.

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

}

Has anyone else run into issues like this? I have verified that the Configs collection has documents available to be returned. My angularfire2 version is 5.0.0-rc.4.

I've also tried using snapshotChanges and getting the specific document from the collection, all are null on first load and work on refresh.

Upvotes: 6

Views: 768

Answers (1)

JoeKer
JoeKer

Reputation: 71

We ended up throwing an error if it didn't return us data and then retrying.

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

Upvotes: 1

Related Questions