Home > Software engineering >  How to print value from observavle using async pipe
How to print value from observavle using async pipe

Time:05-20

I am trying to print value from my observable class using async pipe, but so far with no luck. I have following function, that returns observable class from my API call:

private getRecordType() {
   return this.getMyData().pipe(
      tap((res: Data) => {
         return res.recordType;
      }),
   );
}

Then I set to this.recordType the value that the getRecordType() function returns:

this.recordType = this.getRecordType();
console.log(this.recordType, 'RECORD_TYPE');
// returns Observable class

Then I try to print it in my template:

<b>{{ recordType | async }}</b>

However this only returns [object Object] in my template. Any suggestions on what am I doing wrong?

Thank you in advance.

CodePudding user response:

tap doesn't change the emission of an Observable, so your tap in this case is doing nothing at all. The operator you're looking for (to modify the emissions of an Observable) is map.

CodePudding user response:

The tap operator allows you to perform side-effects on your observable (see the documentation). Returning res.recordType into the tap operator won't do what you want

I suggest you to use the map operator.

  • Related