error TS2339: Property 'catchError' does not exist on type 'Observable<any>'

别来无恙 提交于 2019-11-30 18:47:28

问题


Here is my code in book.service.ts :

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

//import { Component, OnInit } from '@angular/core';
//import {HttpClient} from "@angular/common/http";
//import { Observable } from 'rxjs/Observable'; 
//import 'rxjs/add/operator/map';
//import 'rxjs/add/operators/catch';
//import 'rxjs/operators/toPromise';

@Injectable()
export class BookService 
{
    url = "http://localhost:4200/assets/data/books.json";

    constructor(private http:Http) { }

    getBooksWithObservable(): Observable<Book[]> 
    {
        return this.http.get(this.url)
                .pipe(map(this.extractData))
                .catchError(this.handleErrorObservable);
    }
    getBooksWithPromise(): Promise<Book[]> 
    {
        return this.http.get(this.url).toPromise()
            .then(this.extractData)
            .catch(this.handleErrorPromise);
    }
    private extractData(res: Response) 
    {
        let body = res.json();
        return body;
    }
    private handleErrorObservable (error: Response | any) 
    {
        console.error(error.message || error);
        //console.log("Error in Observable");
        return Observable.throw(error.message || error);
    }
    private handleErrorPromise (error: Response | any) 
    {
        console.error(error.message || error);
        return Promise.reject(error.message || error);
    }   
}

And I m getting the Error here:

ERROR in src/app/book.service.ts(26,18): error TS2339: Property 'catchError' does not exist on type 'Observable'.

Well the Error is in 26th line and that is :

.catchError(this.handleErrorObservable); 

I've tried a lot of things but nothing worked... Can anybody solve this?

Did with 'catch' but didn't work, so then I go for 'catchError' but still, there is this error...


回答1:


catchError needs to be imported and then used within .pipe:

import {catchError} from 'rxjs/operators/catchError'; 

return this.http.get(this.url)
            .pipe(
              map(this.extractData),
              catchError(this.handleErrorObservable)
            );



回答2:


You need to use it inside pipe like this:

getBooksWithObservable(): Observable<Book[]> {
  return this.http.get(this.url)
             .pipe(map(this.extractData),catchError(this.handleErrorObservable));
}



回答3:


catchError must be in pipe.

 import { Observable, pipe } from 'rxjs';
 import { map, catchError } from 'rxjs/operators';

 getBooksWithObservable(): Observable<Book[]> 
    {
        return this.http.get(this.url)
                .pipe(
                   map(this.extractData),
                   catchError(this.handleErrorObservable)
                 );                    
    }


来源:https://stackoverflow.com/questions/50600770/error-ts2339-property-catcherror-does-not-exist-on-type-observableany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!