2017-08-09 23 views
2

私はサービスをAngular 4.3から新しいHttpClientに切り替えました。角度4.3 HttpClientレスポンスObj

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

しかし、応答が<ValidateEmailResponse>

などの種類<Object>としないです。

enter image description here

宣言され、それはすべきではありませんか?

+0

どのようなツールがこのメッセージを提供していますか?それはそのようなオブジェクトをすべて列挙していますか?前の行では正しいタイプを表示しているようです。 – DeborahK

+0

ブラウザ内で、VSCodeデバッガ内にある私のスクリーンショットから。私は、成功後にValidateEmailResponseのインスタンス用の単純なconsole.logを追加し、ログをスキップします。画像としての前の行はコンソールログValidateEmailResponse success - > objectです。 –

答えて

1

これは、私のコードが新しい4.3 HttpClient(以下を参照)のようになりました。私のメソッドの戻り値の型に注目してください。

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); 
} 
+0

MyメソッドはObservableを返します。 それでもObjectを返しています。 私は試したことがありません:.do(data => console.log( 'All:' + JSON.stringify(data))) 私はこれが必要だとは思わないので。 –

+0

いいえ、あなたは 'do'は必要ありません。これはデバッグ用です。 – DeborahK

+0

これは、キャッチのために私の互換性のないタイプのエラーを解決するのを助けました:) – Guntram

関連する問題