0
votes

I have a method in a service which returns an Observable. When I subscribe to the observable when I call the method in my component everything works fine and i get my account properly:

this.service.getAccountDetails(username)
    .subscribe(account =>{
        this.podcasts.push(account);
    });

However when I tried to implement this using a pipe with a map and catchError it returns nothing.

this.service.getAccountDetails(username)
    .pipe(
        map(account => {
            this.podcasts.push(account);
        }),
        catchError((error: Error) => {
            //Do some error stuff
            return of(null);
        })
    );

The method in the service I call looks like this:

getAccountDetails(u:String): Observable<Object> { }

Obviously I must have some annoying syntax error, but for the love of God I can't find it. Please help!

2
You must subscribe(), otherwise the request is never sent, and the pipeline of operations is never executed. map() is supposed to transform an event into something else, not to trigger side-effects. - JB Nizet
You're not returning anything from map() - martin

2 Answers

1
votes

Change it to something like this, as JB Nizet mentioned in the comment, you must subscribe,

this.service.getAccountDetails(username)
.pipe(
  catchError(this.errorHandler)
)
.subscribe((username) => this.podcasts.push(username););

and

   errorHandler(error: Error): Observable<any> {
    console.error('ERROR HANDLER', error);
    return new EmptyObservable();
  }
1
votes

You don't need a map over here unless you are not transforming the result

this.service.getAccountDetails(username)
    .pipe(
        catchError((error: Error) => {
            //Do some error stuff
            return of(null);
        })
    );

this.service.getAccountDetails(username)
    .subscribe(account =>{
        this.podcasts.push(account);
    });