Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 798
JoeKer Avatar asked Feb 15 '18 16:02

JoeKer


1 Answers

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);
like image 190
JoeKer Avatar answered Oct 26 '22 19:10

JoeKer