My Angular database service has been set up in this manner since Angular 6, and I am now encountering this error for the first time.
I have a standard HTTP POST method that returns a result. In case of an error, I want to handle it appropriately. The most common error being an expired token, which is handled specifically to log out the user.
POST
postUsp(username): Observable<ProcResult[]> {
const url = hosturl +'u_sp';
const httpOptions = {
headers: new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Authorization': ls || '{}'
}),
withCredentials: true,
params: {
'username': username,
}
};
return this._http.post<ProcResult[]>(url, null, httpOptions)
.pipe(
map((res) => {
return <ProcResult[]> res;
}),
catchError(this.handleError)
)
}
handleError
private handleError(error: Response | any) {
if (error.status === 401) {
console.log('Signing out.')
try {
Auth.signOut().then(() => {
window.location.reload()
})
return error
} catch (error) { console.log('error signing out: ', error); }
} else {
return throwError(error);
}
}
Full error output:
Error: src/app/db.service.ts:303:3 - error TS2322: Type 'Observable<unknown>' is not assignable to type 'Observable<ProcResult[]>'.
Type 'unknown' is not assignable to type 'ProcResult[]'.
303 return this._http.post<ProcResult[]>(url, null, httpOptions)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
304 .pipe(
~~~~~~~~
...
309 catchError(this.handleError)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
310 )