Midhilaj
Midhilaj

Reputation: 4987

How to make firebase data read only once in typescript

readProductFromServer() {

    this.qS = this.afDatabase.list('product_table', ref => {

      return ref.limitToLast(1000) ;


    }).snapshotChanges().map(changes => {
      return changes.map(c => ({ key1: c.payload.key, ...c.payload.val() }));
    });
    this.qS.subscribe(values => {

    });
  }

How to make this code read only once. I searched on the internet and not get an answer to my code, some solution is available in StackOverflow but not match with my code?

Upvotes: 0

Views: 182

Answers (1)

Quentin Fonck
Quentin Fonck

Reputation: 1313

Since it is an observable, if you want to read your data only one time and don't get any new emissions of possible update, juste add take(1) in your operator chains:

this.qS = this.afDatabase.list('product_table', ref => {
    return ref.limitToLast(1000) ;
}).snapshotChanges().map(changes => {
    return changes.map(c => ({ key1: c.payload.key, ...c.payload.val() }));
}).take(1);

this.qS.subscribe(values => {

});

It will take the first emission then complete the observable.

Upvotes: 1

Related Questions