Angular 2 redirection in http / rxjs catch callback raises TypeError: unable to read subscribe property undefined

When redirecting from catch rxjs function, I get the error: EXCEPTION: TypeError: Cannot read property 'subscribe' of undefined

Here is the relevant part of contactService :

 getContacts () { ... return this.http.get(this.apiUrl, options) .map(this.extractData, this) .catch(err => { this.handleError(err) }) } ... handleError (error) { if(error.status === 401){ //the redirect works great, except, when added, I get the exception error this.router.navigate(['/login']) return Observable.of([])//this is my attempt based on link below } else{ // In a real world app, we might send the error to remote logging infrastructure let errMsg = error.message || 'Server error' console.error(errMsg) // log to console instead return Observable.throw(errMsg) } } 

In consuming component:

 ... ngOnInit(){ this.getContacts() } getContacts(){ this.contactService.getContacts().subscribe( (contacts) => { this.contacts = contacts }, (error) => { this.errorMessage = error } ) } 

This question ( Angular 2. How to handle 4xx errors with redirection in Observable? ) Will suggest that I do it right, but maybe m is missing something in my component?

+2
javascript angular rxjs
source share
1 answer

There is no return

 return this.handleError(err) 
0
source

All Articles