0

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?

1
  • Maybe you should add the Module declaration to your post so people stop telling you to provide the services? Commented Nov 20, 2019 at 14:47

4 Answers 4

1

My guess: The scope of this.handleService.error is different when passed as a parameter and not executed (inside of your deleteCustomer function).

Replace it with this.handleService.error.bind(this)

Sign up to request clarification or add additional context in comments.

Comments

1

Have you had your service in your app.module.ts ?

providers: [HandleService,NotificationService], 

2 Comments

It will give error if not provided. It won't undefined I think.
Ye, it is provided. Like i said, its working correctly for the succes method which wouldnt be the case if there was a provider issue.
0

Add service on providers array in App.module.ts, or you can use:

 @Injectable({ providedIn: 'root' }) 

Comments

0

You don't need to add any provider to app.module.ts

Just add decorator ahead your service class

@Injectable({ providedIn: 'root' }) export class CustomerService 

same for HandleService

1 Comment

Only if it is appropriate that the services in question be provided at root, of course.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.