A function has been defined to handle errors by opening a MatSnackBar
to display the error message whenever a get request encounters an error.
handleError(err: HttpErrorResponse) {
this.snackBar.open(err.message)
return throwError(err)
}
When subscribing to an observable in the constructor, this error handling function is passed to the catchError
method.
constructor(private snackBack: MatSnackBar) {
...
this._obv$ = this.myService.getTypes().pipe(
catchError(this.handleError)
)
...
However, when an error occurs, the err
parameter passed to the handleError
function is correct, but the this.snackBar
instance is found to be undefined
.
The issue can be resolved by using the handleError
function in a different way like below:
this._obv$ = this.myService.getTypes().pipe(
catchError(err => this.handleError(err)
)
What's causing the difference and why is the snackBar
instance turning out to be undefined
?