Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: rxjs__WEBPACK_IMPORTED_MODULE_2__.Observable.throw is not a function

Here is my file userdata.service.ts :

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import {Observable} from 'rxjs';
import { map } from "rxjs/operators";
import { catchError } from 'rxjs/operators';
import { Data } from './userdata';

@Injectable()
export class UserDataService {

  url : "http://localhost:4200/assets/data/userdata.json";
  constructor(private http:Http) { }  

  getDataWithObservable() : Observable<any>{
      return this.http.get(this.url)
            .pipe(
              map(this.extractData),
              catchError(this.handleErrorObservable)
            );
  }

    private extractData(res: Response) 
    {
        let body = res.json();      
        return body;
    }

    private handleErrorObservable (error: Response | any) 
    {   
        return Observable.throw(error.message || error);
    }
}

I m getting the following Error :

TypeError: rxjs__WEBPACK_IMPORTED_MODULE_2__.Observable.throw is not a function

like image 468
Jitendra Ahuja Avatar asked May 30 '18 10:05

Jitendra Ahuja


2 Answers

Import:

import { throwError } from 'rxjs';

Usage:

return throwError(err);
like image 85
Chris Fremgen Avatar answered Oct 23 '22 05:10

Chris Fremgen


Observable.throw() has been deleted.

Try using Observable.throwError(). Apparently the definitions aren't up to date just yet.

You can import it using import { throwError } from 'rxjs';

like image 20
Kaedeko Avatar answered Oct 23 '22 06:10

Kaedeko