0

When I do HTTP requests, for example a GET request and that i got a status code 400 for invalid given data, it prevents my HTML template to be displayed in the view of my angular app. This is how I deal with it at the moment :

this.myService.getData(neededData)
  .subscribe(
    (result) => {
      this.data = result;
    },
    (error) => { 
      if (error) { 
        console.log("error"); 
        this.anErrorHasOccured = true; 
      }
    }
  );

The 'error' is well displayed in the console, but this error prevents my template to be displayed, how can I fix that ?

1 Answer 1

2

Could you try with this :

this.myService.getData(neededData)
  .pipe(catchError(err => {
    console.err(err);
    return of([]);
  }))
  .subscribe(
    (result) => this.data = result,
  );
Sign up to request clarification or add additional context in comments.

4 Comments

thx for helping me, this is the error i got from VScode with this code : Argument of type '(err: any) => void' is not assignable to parameter of type '(err: any, caught: Observable<Object>) => ObservableInput<any>'. Type 'void' is not assignable to type 'ObservableInput<any>'.
import { catchError } from 'rxjs/operators';
You have correclty imported { catchError } from 'rxjs/operators'; ?
Yeeaah great ! :D

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.