Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

javascript - angular http call is not handling error when with error handler written in subscription

I have the following code

ngOnInit(): void {
    this.route.params
      .subscribe(
        (param:Params) =>{
          this.http.get(environment.creator_by_url_slug + param.creatorurlslug)
            .subscribe((req: any)=>{
              this.creatorobject = req;
            });
        }, error => {
          console.log('this is the error');
          console.log(error);
        }
      );
  }

however when I get a 404 back from my server, the error handler is not called.

I am not getting console output

is there something wrong with my syntax?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

it was a mistake in brackets. How awesome. Im on my 4th monster but got it figured out. Here is the correct code:

ngOnInit(): void {
    this.route.params
      .subscribe(
        (param:Params) => {
          this.http.get(environment.creator_by_url_slug + param.creatorurlslug)
            .subscribe((req: any) => {
                this.creatorobject = req;
              },
              (error) => {
                if (error.status == 404) {
                  this.router.navigate(['404']);
                }
              });
        });
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...