Previously my project was on angular2. Now its upgraded to angular6. I am facing some issues regarding the version changes. I will get some json data from an api and I am accessing it through the @angular/http. Following are the angular service I've tried
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
constructor(private http: HttpClient, @Inject('api') private serverApi) { }
public getResultList(): Promise<any> {
let uri = `${this.serverApi}/list`;
return this.http.get(uri)
.toPromise()
.then((res) => {
let temp = res.json();
return { 'statusCode': 200, 'message': 'success', 'result': temp['items'], 'error': {} };
})
.catch((error) => {
return { 'statusCode': 200, 'message': 'failed', 'result': {}, 'error': error };
})
}
I would like to replace the above code to angular6. Following are the code I've tried.
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient, @Inject('api') private serverApi) { }
public getResultList(): Observable<any> {
let uri = `${this.serverApi}/list`;
return this.http.get<any[]>(uri)
.pipe(
tap((res) => {
let temp = res;
return { 'statusCode': 200, 'message': 'success', 'result': temp['items'], 'error': {} };
}),
catchError((error) => {
var data = { 'statusCode': 200, 'message': 'success', 'result': {}, 'error': error };
this.handleError('getResultList', data)
})
);
}
private handleError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.error(error); // log to console instead
return of(result as T);
};
}
Here I am getting the response from the server side but not able to customise the get result and return it from the tap itself. Please help me to resolve the same.