A
A
asd dsa2018-11-30 22:17:14
JavaScript
asd dsa, 2018-11-30 22:17:14

How to solve TypeScript error?

In the "saveExpense" method, when passing error handling "catchError(this.errorHandlerService.handleError)", this method is highlighted and the error is displayed: Argument type (error:HttpErrorResponse)=>Observable is not assignable to parameter type (err:any, caught:Observable )=> never . How to decide?

saveExpense(userid, oExpense): Observable<any> {
        const httpOptions = {
            headers: new HttpHeaders({
                'Content-Type': 'application/json',
                'Authorization': `${this.jwtToken}`
            })
        };
        return this.http.post(`http://localhost:5555/api/expense/${userid}`, JSON.stringify(oExpense), httpOptions).pipe(
            tap(
                (response: any) => console.log(response)
            ),
            catchError(this.errorHandlerService.handleError)
        );
    }

export class ErrorHandlerService {

    handleError(error: HttpErrorResponse) {
        console.error(error);
        return throwError(error.error || 'Server error');
    }
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2018-11-30
@yaNastia

catchError accepts a function like (err:any, caught:Observable)=> never
And you are trying to pass (error: HttpErrorResponse)

M
msdosx86, 2018-12-01
@msdosx86

The compiler already shows you an error, but you can't read

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question