Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 699
Doflamingo19 Avatar asked Sep 21 '18 09:09

Doflamingo19


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.

like image 151
Vinod Bhavnani Avatar answered Nov 01 '22 02:11

Vinod Bhavnani