Angularfire: subscribe is not working in the second time

Created on 21 Sep 2018  路  5Comments  路  Source: angular/angularfire

Hi i am using firestore through angularfire2 i want to call a collection once the app loaded and want to store in a observable variable and want to use through out the app below is my code.

in constructor

 fleets: Observable<Fleet[]>;
this.fleets =this.afs.collection('Fleets').snapshotChanges().pipe(map(
      changes => {
        return changes.map(
          a => {
            const data = a.payload.doc.data() as Fleet;
            data.id = a.payload.doc.id;
            return data;
          });
      }));

getflleet()
{
return this.fleets;
}

in more than one component

this.firebaseService.getflleet()
     .subscribe(
       (fleet) => {
         this.arr = fleet;
         console.log(this.arr);
       }
     );
   }

but the problem is only once a time its calling in the first component.
Thanks in andvance

Most helpful comment

Indeed, a BehaviorSubject is what you wanna use.

All 5 comments

Yes, it doesn't trigger for second time changes. Any help please....

I started to learn about RxJS recently and I don't think it is an appropriate way to use Subjects like this but I am using Subjects on services to hold and serve data. Not the best but surely works for me. It works when you revisit component because BehaviorSubject always have recently passed value.

Indeed, a BehaviorSubject is what you wanna use.

I suggest using the shareReplay operator in this case.

RXJS things. Working as intended. Closing.

Was this page helpful?
0 / 5 - 0 ratings