Doflamingo19
Doflamingo19

Reputation: 1629

rxjs__WEBPACK_IMPORTED_MODULE_3__.Observable.throw is not a function at

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?

Upvotes: 8

Views: 4175

Answers (1)

Vinod Bhavnani
Vinod Bhavnani

Reputation: 2225

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.

Upvotes: 14

Related Questions