Aymen Kanzari
Aymen Kanzari

Reputation: 2013

observable.throw is not a function

I tried to implement catch handling in a service, but i've got an undefined function error in my console.

This is the error message:

TypeError: rxjs_internal_Observable__WEBPACK_IMPORTED_MODULE_2__.Observable.throw is not a function

import { Injectable } from '@angular/core';
import { config } from '../../../config/config';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { HttpResponseHandler } from './http-response-handler.service';
import { Observable } from 'rxjs/internal/Observable';

@Injectable()
export class DataService<T> {

constructor(
        protected url: string,
        protected httpClient: HttpClient,
        protected responseHandler: HttpResponseHandler
    ) { }

    getOneById<T>(id): Observable<T> {
        return this.httpClient
            .get<T>(config.ecmBackUrl + this.url + '/' + id)
            .pipe(catchError((err, source) => this.responseHandler.onCatch(err, source)));
    }
}

@Injectable()
export class HttpResponseHandler {

    public onCatch(response: any, source: Observable<any>): Observable<any> {
       console.log(response);
    }
}

Upvotes: 0

Views: 2010

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41387

change the import path of the Observable

from

import { Observable } from 'rxjs/internal/Observable';

to

import { Observable } from 'rxjs/Observable';

Upvotes: 1

Related Questions