Adam Šulc
Adam Šulc

Reputation: 546

How to print value from observavle using async pipe

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.

Upvotes: 0

Views: 661

Answers (2)

sebastiencn
sebastiencn

Reputation: 248

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.

Upvotes: 1

Will Alexander
Will Alexander

Reputation: 3571

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.

Upvotes: 1

Related Questions