I encountered an issue while upgrading from Angular v4 to Angular v6. I was in the process of replacing Http
and HttpModule
with HttpClient
and HttpClientModule
. As a result, I imported HttpClient
from @angular/common/http
in a service to fetch results from DBpedia's API. Previously, I utilized Http
from @angular/http
and my code functioned correctly.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Jsonp, Headers, RequestOptions, URLSearchParams} from "@angular/http";
import {Store} from "@ngrx/store";
import * as fromRoot from '../reducers';
import {Observable} from "rxjs";
@Injectable()
export class KnowledgeapiService {
server = 'http://lookup.dbpedia.org';
searchURL = this.server + '/api/search/KeywordSearch?';
homepage = 'https://susper.com';
logo = '../images/susper.svg';
constructor(
private http: HttpClient,
private jsonp: Jsonp,
private store: Store<fromRoot.State>
) {}
public getsearchresults(searchquery){
let params = new URLSearchParams();
params.set('QueryString', searchquery);
let headers = new Headers({ 'Accept': 'application/json' });
let options:any = new RequestOptions({ headers: headers, search: params });
return this.http
.get(this.searchURL, options).map(res =>
res.json()
).catch(this.handleError);
}
private handleError (error: any) {
// In some advance version we can include a remote logging of errors
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // Right now we are logging to console itself
return Observable.throw(errMsg);
}
}
However, after switching from using Http
to HttpClient
, I encountered an error in the getsearchresults(searchquery)
function. Whenever I tried to use the map
function to map data to JSON, it threw an error stating that the map
function does not exist on type Observable<ArrayBuffer>
. Removing the map
function resulted in a similar message for the catch
function. I referred to https://github.com/angular/angular/issues/15548 but the suggested solution did not work for me. Can someone point out where I might be going wrong? Should I consider removing both the map
and catch
functions in getsearchresults(searchquery)
?