0

i created a service by driving my service from

EntityCollectionServiceBase<Transaction>

My entity is called Transaction.

i am overriding the getAll method like this

  getAll(): Observable<Transaction[]> {
return this.http
  .get<ApiResponse>('https://localhost:xxxx/transaction/GetLastSixMonth')
  .pipe(
    tap((data) => {
      this.notifyService.showSuccess(data.message, 'Sucess');
    }),
    map((res) => res.result),
    catchError((err) => {
      this.notifyService.showError(
        'Error While Six Month Transactions',
        'Error'
      );
      return of();
    })
  );
}

i am calling this getAll somewhere appropriate.

so in the component where i want to use it i am using the "entities$" property of this service like this

this.last6MonthWithdrawCount$ = this.transactionsDataService.entities$.pipe(
  map((transactions) => {
    const res = transactions.filter(
      (transaction) =>
        transaction.transactionType === TransactionType.Withdraw
    ).length;
    return res;
  })
);

so this variable "this.last6MonthWithdrawCount$ " works fine when i use it in html using asyn operator.

{{ last6MonthWithdrawCount$  | async }}

but when i want to read its value in the code by subscribing to it

this.last6MonthWithdrawCount$.subscribe(x => this.withdrawCount = x);

i dont get proper value. i get 0 . looks like it is giving me first value in the stream.

how to get the last value ? UPDATE or simply how can i get the value from this.last6MonthWithdrawCount$ in the code. actually this value is supposed to be passed to a graph data. i have graph that shows the value and also i have to pass the same value in data array of a graph.

enter image description here

this value 45 (same sort of stuff for the value 10) is coming from entities$.pipe code mentioned above and in the html i am doing a simple | async as mentioned above, and it works.

i have to pass the same 2 values in data of my chartJS chart.

  this.myChartData.data.datasets[0].data = [this.depositCount, this.withdrawCount];

how to get this value from that observable ? the same that is working in html.

Raas Masood
  • 1,475
  • 3
  • 23
  • 61
  • We correctly understand that 0 is the initial value, since initially it is an empty array but How to understand the last value of observable, entity$ can change an unlimited number of times and the last value will be different numbers and each time the variable withdrawCount will change. You can update every case. Сould you clarify what you want to get – bitvalser Jun 22 '20 at 21:41
  • when i simply use ngrx with normal actions, reducers, effects and selectors without using NGRX entity data service, i subscribe to an observable using store something like this.store .select(someSelctor) .subscribe it works. now using entity collection service provided by ngrx we dont have any selectors or actions as such. correct, i get value from a store using entities$ property. this works fine when i use that observable (that was fetched using entities$) in html but doesn work when i subscribe to the same thing in code. – Raas Masood Jun 23 '20 at 01:02
  • i updated the question with more details. – Raas Masood Jun 23 '20 at 01:12

0 Answers0