Jordan Lewallen
Jordan Lewallen

Reputation: 1851

RxJS combineLatest not emitting properly after updating observable

I have the following RxJS combineLatest that waits until three objects are returned from my Firebase Firestore (user, artist, release) of which i combine and flatten into an array of objects:

combineLatest([this.release$, this.artist$, this.auth.user$]).subscribe(
  (results) => {
    console.log(results)
    this.releasePage = true;
    var releaseArray = this.helper.flattenArray(results);
    console.log(releaseArray);
  }
)

When I load this page and console.log the releaseArray (which has the flattened object array) I can see all 3 objects as expected.

Now here's where the issue arises. The user can edit the release info, but when the user saves the updates, I get an error about a property in the release object not being defined. And when I look at the log for the release array, only the artist and user objects are returned in the new releaseArray. Why isn't the release object returned in this object array as well?

Here's an image, the first two log statements are on page load, and the last two log statements are shown once Firestore has updated data, thus emitting since values changed:

enter image description here

When I go in to Firestore and directly update a property of the object, the observable returns 3 updated objects as it should.

For what it's worth here is my Firestore AngularFire2 update code used to update an object:

this.db.updateAt(`submissions/${this.submission.id}`, {
        ...form.value
})

And the updateAt method in my db Service:

  updateAt(path: string, data: Object): Promise<any> {
    const segments = path.split('/').filter(v => v);
    if (segments.length % 2) {
      // Odd is always a collection
      return this.afs.collection(path).add(data);
    } else {
      // Even is always document
      return this.afs.doc(path).set(data, { merge: true });
    }
  }

Let me know what else you need to help solve this, thanks!

Upvotes: 1

Views: 533

Answers (1)

Jordan Lewallen
Jordan Lewallen

Reputation: 1851

It was just me being dumb, when I submit the page with updates, the release id was being updated as well as the updated content so the observable couldn't find what it had initially been tasked to be observed....

I added:

if(!this.submitForm.value.submissionUID) {
  this.submitForm.value.submissionUID = Math.random().toString(36).substring(2);
}

And this took care of reupdating the submissionUID if it already existed....

Upvotes: 1

Related Questions