3

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?

2
  • What tool is providing that message? Does it list all objects like that? Because on the prior line it seems to display the correct type. Commented Aug 9, 2017 at 16:33
  • Within the browser, but from my screen shot that is within VSCode debugger. I've added a simple console.log for instance of ValidateEmailResponse after the success, and it skips the log. The prior line as the picture is just a console log ValidateEmailResponse success --> object. Commented Aug 9, 2017 at 16:47

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

My Method returns Observable<ValidateEmailResponse> But it is still returning Object. I have not tried: .do(data => console.log('All: ' + JSON.stringify(data))) As I do not believe I need this.
No, you don't need do. That's just for debugging.
This helped me out solving my incompatible types error for the catch :)
Is there an import statement for Error from err.error instanceof Error, I've been having trouble getting if branch to fire when disabling the network in Chrome, which I thought would be of type Error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.