rxjs__WEBPACK_IMPORTED_MODULE_3__.Observable.throw is not a function at

Viewed 3895

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?

1 Answers

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.

Related