0

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
  • 1
    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. Commented Nov 4, 2018 at 18:46
  • You're not returning anything from map() Commented Nov 4, 2018 at 18:54

2 Answers 2

1

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();
  }
Sign up to request clarification or add additional context in comments.

1 Comment

forgive me for i am a dumbass
1

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);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.