I have the following HandleService:
@Injectable() export class HandleService { constructor(private notificationService: NotificationService) { } success(message: string) { this.notificationService.printSuccessMessage(message); } error(error: HttpErrorResponse) { if (error.error instanceof ErrorEvent) { // A client-side or network error occurred. Handle it accordingly. this.notificationService.printErrorMessage(error.error.message); console.error('An error occurred:', error.error.message); } else { // The backend returned an unsuccessful response code. // The response body may contain clues as to what went wrong, this.notificationService.printErrorMessage(error.message); console.error( `Backend returned code ${error.status}, ` + `body was: ${error.error}`); } // return an observable with a user-facing error message return throwError( 'Something bad happened; please try again later.'); }; } And thats the used NotificationService:
@Injectable() export class NotificationService { private _notification: any = alertify; constructor() { } openConfirmationDialog(message: string, okCallback: () => any) { this._notification.confirm(message, function (e) { if (e) { okCallback(); } else { } }); } printSuccessMessage(message: string) { this._notification.success(message); } printErrorMessage(message: string) { this._notification.error(message); } openAlertDialog(message: string) { this._notification.alert(message); } } I use HandleService like this:
@Injectable() export class CustomerService { constructor(public http: HttpClient, private authService: AuthService, private handleService: HandleService) { } deleteCustomer(customer: Customer): Observable<any> { return this.http.delete(BASE_URL + customer.id, this.authService.setHeaders('application/json')) .pipe( tap(() => this.handleService.success(`Deleted customer: ${customer.name}`)), catchError(this.handleService.error) ); } } So when everything works fine this.handleService.success('Deleted customer: ${customer.name}') is executed which works as expected. If an error occurs, this.handleService.error is executed, but it does not print the error message, instead i get the following error cannot read property 'printErrorMessage' of undefined. So notificationService in my handleService is undefined here, but why? Its injected in the constructor and it works correctly on the succes-method. Why isnt it working in the error-method?