Taranjit Kang
Taranjit Kang

Reputation: 2580

Angular 4.3 HttpClient Response Obj

I've been switching my services to the new HttpClient from Angular 4.3.

return this._http.get<ValidateEmailResponse>(`${_validateEmailApi}`, { params })
    .catch(this.handleError);

However, the response is of type <Object> and not of <ValidateEmailResponse>

such as;

enter image description here

Should it not be as declared?

Upvotes: 3

Views: 526

Answers (1)

DeborahK
DeborahK

Reputation: 60518

This is what my code now looks like with the new 4.3 HttpClient (see below). Notice the return type of my method.

getProducts(): Observable<IProduct[]> {
    return this._http.get<IProduct[]>(this._productUrl)
        .do(data => console.log('All: ' + JSON.stringify(data)))
        .catch(this.handleError);
}

private handleError(err: HttpErrorResponse) {
    // in a real world app, we may send the server to some remote logging infrastructure
    // instead of just logging it to the console
    let errorMessage = '';
    if (err.error instanceof Error) {
        // A client-side or network error occurred. Handle it accordingly.
        errorMessage = `An error occurred: ${err.error.message}`;
    } else {
        // The backend returned an unsuccessful response code.
        // The response body may contain clues as to what went wrong,
        errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
    }
    console.error(errorMessage);
    return Observable.throw(errorMessage);
}

Upvotes: 2

Related Questions