[Fixed] Angular Injectable Toastr Service is undefined


I created a custom Toastr service (more of a wrapper class if you will). I inject this in my data service (api calls) but every time I call it I get an error that its undefined. If I use this service in a component then it works fine.

I think it might have to do how I use it in my Put call.

How I export my ToastrWrapperClass

export class ToastrAlertService {

I inject it here and it references my correct service

export class UserService {

        private http: HttpClient,
        private apiEndpointService: ApiEndpointsService,
        private adapter: UserAdapter,
        private toastrAlertService: ToastrAlertService,
    ) { }

Here I call my Handle Error (I think it might be by how I am handling the ‘catchError’ here

public put(model: User): Observable<User> {
    const headers = new HttpHeaders()
        .set('Content-Type', 'application/json');

    const putModel = {
        Model: model,

    return this.http.put(this.apiEndpointService.apiUrl(this.apiType + '/'), JSON.stringify(putModel), { headers }).pipe(
        map((data: any) => this.adapter.adapt(data)), catchError(this.handleError),

This toastrAlertService is ‘undefined’ when the error gets returned from my API

handleError(error) {
    return throwError(error);


Handling the catchError as follows solved it for me. (I did move the handle error logic to a single class to make the code cleaner)

 return this.http.put(this.apiEndpointService.apiUrl(this.apiType + '/'), JSON.stringify(model), { headers }).pipe(
        map((data: any) => this.adapter.adapt(data)),
        catchError(err => {
            return of(null);

Leave a Reply

(*) Required, Your email will not be published