Angular 4: How to call catch and callback function in it to handle error. Arrow function, manage errors in get or post services.

How to call catch and callback function in it to handle error

Angular 4: How to call catch and callback function in it to handle error

Angular 4: how to redirect if API returns an error?

How to manage errors in get or post services

Need to import

import { Router } from ‘@angular/router’;

import { Observable } from ‘rxjs/Rx’;

 Need to mention router in constructor

constructor(private router: Router, private http: Http) {}

Sometimes when you call catch without using arrow function like below

getUserList() {

                return this.http.get(this.constURL + ‘/loans/all’, this.headerOptions)

                .catch(this.handleError);

}

 

handleError(error: Response) {

                if (error.status == 500) {     

                  this.router.navigate([‘/login’]);

                } else {

                  return Observable.throw(error);

                }

}

 

Then it gives error of

 

ERROR TypeError: Cannot read property ‘navigate’ of undefined not getting this

 

Because in handleError function this object is not accessible. If you console this.router then you will get undefined.

So this object not working and not getting router all available methods

So you have to use arrow function here like below

 

getUserList() {

                return this.http.get(this.constURL + ‘/loans/all’, this.headerOptions)

                .catch(error => {

                  return this.handleError(error);

                });

}

 

handleError(error: Response) {

                if (error.status == 500) {     

                  this.router.navigate([‘/login’]);

                } else {

                  return Observable.throw(error);

                }

}

 

Also if you have not mentioned return for handlerError function then it will throw error again like

Argument of type ‘(error: any) => void’ is not assignable to parameter of type

So it’s necessary to type return for handlerError function.

 

I searched a lot for my solution after spending half a day i can able to find solution.

Nowhere given simple/full solution.

24 thoughts on “Angular 4: How to call catch and callback function in it to handle error. Arrow function, manage errors in get or post services.

  1. I just wondered if you’ve planned any marketing yet for your site. I’m self-employed doing this for various businesses for a number of years now, I feed my family doing this so I won’t complain. I have a means of getting immediate interested traffic and buyers to your website through social media channels and email. Along with getting more likes, followers for all of your social media accounts. I have a brand new program that’s just been completed that listens to any or all social mentions being made, if a certain word or phrase is detected, we instantly send back to them a message that they should visit your site. We could use as numerous search terms as we wish, hundreds of targeted visits a day. I can even help you to make/update your site, fix site errors add updates etc. If you might need it.

    Along with that, I’d also like to find out what your competitors have implimented that we havn’t done yet and address those issues asap. I’d also like to produce a video or two about your website and encourage them to rank high pretty quickly. Lastly I’ve a sizable database of opt in customers that are enthusiastic about what it is you do, so if you’d like to expand your present newsletter list i’d like to know, I can allow you to get these records whenever you’d like them. They’d allow you to get instant leads by supplying you with a listing of people or businesses that are looking for exactly what it is you’re offering.

    I personally use tools that a lot of people don’t find out about or don’t have time to use for themselves and I would like to utilize them for your site. If your’re to busy with current clients I get it, I was just wondering was all. Let me know if you’d like more info or references, I do have more than I know what to do with.

    Winston
    1.319.423.9473

  2. We are a group of volunteers and starting a new scheme in our community.
    Your site offered us with valuable information to work on. You’ve done a formidable job and our entire community
    will be grateful to you.

  3. Hello There. I found your weblog the use of msn. That is a very well written article.
    I will be sure to bookmark it and return to read more of
    your useful information. Thanks for the post.
    I will definitely comeback.

  4. You really make it seem so easy with your presentation but
    I find this matter to be actually something which
    I think I would never understand. It seems too complex and extremely
    broad for me. I am looking forward for your next post, I will try to get the hang of it!

  5. Hey! Someone in my Myspace group shared this site with us so I came to give it a
    look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers!
    Outstanding blog and great design.

Leave a Reply