2017-10-05 2 views
0

私が定義したオブザーバブルオブジェクトでAngular4 HttpClientを使用しようとしています。残念ながら、私はオブジェクトへの応答をマップすることはできません。応答をオブジェクトにマップできません

問題は、httpclient(これはjsonを暗黙的に返すので、response.json()関数にはありません)を使用しているようですが、httpが非推奨になっていることを理解していますか?とにかくこれが原因でresponse.json()はエラーを引き起こします。 エラーTypeError:response.jsonは関数ではありません

コードです。

import { Injectable } from '@angular/core'; 
import { HttpClient } from '@angular/common/http'; 
import 'rxjs/add/operator/map'; 

import {Observable} from 'rxjs/Observable'; 
import {JsonpModule, Jsonp, Response} from '@angular/http'; 

export class BucketList { 
    constructor(public name: string, 
       public creationdate: string) { 
    } 
} 

@Injectable() 
export class DocumentSearchService { 

    constructor(private _http : HttpClient) { } 

    getBucketList() : Observable<BucketList> { 

     let serviceURL = "http://localhost:3000/listBuckets"; 

     return this._http.get(serviceURL, {withCredentials: true, responseType: 'json'}) 

     .map((response: Response) => <BucketList>(response.json()) 
     .catch((error: any) => window.console.log(error))); 
    } 
} 

ngOnInit() { 
// this.BucketList = this.DocumentSearchService.getBucketList 

    this.BucketList = 
    this.DocumentSearchService.getBucketList().subscribe(value => { 
} 

誰かが正しい方向に私を向けることができますか? Googleでの検索と検索はこれまでのところ答えが得られていません...

ありがとうございました。彼らはby defaultを暗示しているため

+0

HTTPに.get (のserviceURL) – jitender

+0

ためhttps://angular.io/guide/http –

答えて

0

responseType: 'json'response.json()を省略することができます。

The responseType value determines how a successful response body will be parsed. If responseType is the default json, a type interface for the resulting object may be passed as a type parameter to request().

<...>

get<T>(url: string, options: { 
    headers?: HttpHeaders, 
    observe: 'events', 
    params?: HttpParams, 
    reportProgress?: boolean, 
    responseType?: 'json', 
    withCredentials?: boolean, 
    }): Observable<HttpEvent<T>> 

Construct a GET request which interprets the body as JSON and returns the full event stream.

+0

本当にありがとうございました皆をお読みください入力。 {} getBucketList():今@Injectable() 輸出クラスDocumentSearchService { コンストラクタ(のHttpClientプライベート_HTTP)::-)本当によく働いている// localhostを:3000観察可能 { はのserviceURL =」HTTPを聞かせて/ listBuckets "; \t これを返します._http.get (serviceURL); } } –

+0

よろしくお願いいたします。 – estus

関連する問題