Ad

Firestore ValueChanges() Only Emits Once When Using Take(1)

I have a document called booking in the counts collection (counts/booking) of my firestore database. I want to read data from this booking document and listen for value changes too. How can I achieve this using AngularFire2?

I have tried the following code,

this.afs.collection('counts').doc<Count>('booking').valueChanges().pipe(take(1))
      .subscribe(data => {
        if (data) {
          this.newCount = data.newCount;
          this.otherCount = data.otherCount;
        }
      });

This reads the value but doesn't seem to update the values as it changes in the firestore document.

Ad

Answer

Remove the take(1) pipeable operator as this will cause your code to effectively only execute/emit a single (1) time:

this.afs.collection('counts').doc<Count>('booking').valueChanges()
  .subscribe(data => {
    if (data) {
      this.newCount = data.newCount;
      this.otherCount = data.otherCount;
    }
  });

Hopefully that helps!

Ad
source: stackoverflow.com
Ad