I need to do rest operation adn catch erro and I do this:
import { Injectable, } from '@angular/core';
import { Http, Response,Headers} from '@angular/http';
import { map} from 'rxjs/operators';
import { catchError } from 'rxjs/operators';
import { Observable} from 'rxjs';
@Injectable()
export class RESTSERCIVE{
getObject(id: number){
return this.http.get(this.url+"/"+id).pipe(map(
(response: Response)=>{return response.json()},
),
catchError(this.handleErrorObservable)
);
}
handleErrorObservable (error: Response | any)
{
return Observable.throw(error.message || error);
}
}
I don't why it give me this error. Anyone can help me?
Because you are using Angular 6 and RxJS 6, the syntax for throwing erros has changed.
Do it this way instead,
import { throwError } from 'rxjs';
throwError(error.message || error);
Also in RxjS 6, you don't need to convert the response into JSON explicitly. It is done automatically. So you can remove the map from your code.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With