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

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.

You may also like...

24 Responses

  1. Very interesting information!Perfect just what I was searching for!

  2. ost but I was wanting to know if you could write a litte more on this topic? I’d be very thankful if you could elaborate a little bit further. Appreciate it!

  3. It’s very simple to find out any topic on web as compared to books, as I found this article at this web site.

  4. Winston says:

    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

  5. Jade says:

    I think the admin of this web site is in fact working hard in support of his web site, for the reason that here every stuff is quality
    based data.

  6. Russ says:

    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.

  7. Fredric says:

    Thanks for sharing your thoughts about Angular 2/4/5. Regards

  8. Jacki says:

    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.

  9. Philip says:

    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!

  10. Evelyne says:

    I visit everyday some sites and sites to read posts,
    except this webpage presents quality based writing.

  11. Ulysses says:

    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.

  12. I think the admin of this website is really working hard in support of his web
    site, as here every information is quality based data.

  13. May says:

    Everything is very open with a precise explanation of
    the issues. It was definitely informative. Your website is useful.
    Thank you for sharing!

  14. Hello, the whole thing is going well here and
    ofcourse every one is sharing information, that’s truly good, keep up writing.

  15. Heya i am for the first time here. I found this board and I find It really
    useful & it helped me out a lot. I hope to give something back and aid others like you aided me.

  16. Hey, I think your blog might be having browser compatibility issues.
    When I look at your blog site in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, fantastic blog!

  17. It’s an remarkable post designed for all the
    internet visitors; they will take benefit from it
    I am sure.

  18. I would like to thank you for the efforts you have put in writing
    this website. I am hoping to view the same high-grade blog
    posts by you later on as well. In truth, your creative
    writing abilities has motivated me to get my own blog now 😉

  19. Mariano says:

    Your way of describing all in this article is truly nice,
    all be able to without difficulty understand it, Thanks a lot.

  20. Maggie says:

    If some one desires to be updated with newest technologies
    after that he must be pay a quick visit this site and be up to date all the time.

Leave a Reply