Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had executed post service call in angular 6 the service was returning the request as 200-ok, but it could not returning the message to catcherror method. It was showing an error: Observable.throw is not a function.

My Serivce Call code was given below:

import { Injectable } from '@angular/core';
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import { Observable } from 'rxjs';
import { throwError } from 'rxjs';
import { map } from 'rxjs/operators';
import { catchError } from 'rxjs/operators'
import { HttpHeaders } from '@angular/common/http';
import { CatalogLocationConfigurations } from './CatalogLocationConfigurations.model';

@Injectable()
export class ConfigureCatalogLocationService
{
api_url="http://localhost:8080";
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};

constructor(private http: HttpClient) { }



saveCatalogLocation(catalogLocationConfiguration : CatalogLocationConfigurations): Observable<any>{
return this.http.post(`${this.api_url}/Home/ServiceInvokeForSaveAddCatalogLocation`, catalogLocationConfiguration,this.httpOptions).pipe(
catchError(this.handleError));
}

private handleError(err: HttpErrorResponse) {
let errMsg:string='';
if (err.error instanceof Error) {
console.log('An error occurred:', err.error.message);
alert('An error occurred:'+ err.error.message);
errMsg=err.error.message;
}
else {
console.log(`Backend returned code ${err.status}`);
alert("Backend returned Code:"+err.status);
errMsg=err.error.status;
}
return Observable.throw(errMsg);
}

}



Component:
saveCatalogLocation()
{
this.configureCatalogLocationService.saveCatalogLocation(this.catalogLocationConfiguration)
.subscribe((err)=>{
console.log("Status From Service Call:"+err)
});
}

What I have tried:

I don't know whether there is an issue in version of modules.
Posted
Updated 10-Jan-19 21:13pm

1 solution

Quite possibly it's a versioning issue as it works well for me with rx.js 5.5.6.
Still, I've noticed that for some version people do import as
JavaScript
import { Observable } from 'rxjs/Observable';

which might also help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900