S
S
Sergei Abramov2020-04-28 20:12:39
Angular
Sergei Abramov, 2020-04-28 20:12:39

Why is the service not visible?

I want to make an eventHandler for the service:

..
import { ToastrService } from 'ngx-toastr';
...
export class AuthService {
    constructor(
        protected toastr: ToastrService,
        protected http: HttpClient
    ) {
    }

    signUp(data): Observable<HttpResponse<string>> {
        const url = `${this.url}/sign-up`;
        return this.http.post<HttpResponse<string>>(url, data, httpOptions).pipe(
            catchError(this.handleError)
        );
    }

    protected handleError(error: HttpErrorResponse) {
        if (error.error instanceof ErrorEvent) {
            // A client-side or network error occurred. Handle it accordingly.
            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,
            console.error(
                `Backend returned code ${error.status}, ` +
                `body was: ${error.error}`);
            this.toastr.error('Test', 'It\s something testing text');
        }
        // return an observable with a user-facing error message
        return throwError(
            'Something bad happened; please try again later.');
    }

}


The handler works, displays errors in the console, but the toast does not work in the handler. Writes that it cannot call error on a null object. Why doesn't handler see ToastrService ? does it need to be passed to the handler somehow differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Luzanov, 2020-04-28
@PatriotSY

catchError(this.handleError)
Your context is lost .
You can do this:
catchError(error => this.handleError(error))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question