2017-02-02 3 views
-2

私はAPIからjson配列を受け取り、ページに表示しようとしています。私はサービスに何か問題があると確信しています。ここ角度2のAPIからjson配列を受信

getAuctions(): Promise<Auction[]> { 
     return this.http.get(this.auctionsUrl) 
      .toPromise() 
      .then(response => response.json().array as Auction[]) 
      .catch(this.handleError); 
    } 

応答例である

[ 
    { 
    "id": 1, 
    "name": "Antik stol", 
    "description": "En fin antik stol från 1800-talet", 
    "startTime": "2016-12-13T10:00:00", 
    "endTime": "2017-02-23T10:00:00", 
    "imageUrl": "https://thumbs.dreamstime.com/z/antik-stol-som-stoppas-i-sammet-32935346.jpg", 
    "categoryId": 1, 
    "supplierId": 2, 
    "buyNowPrice": 8000, 
    "sold": true 
    }, 
    { 
    "id": 2, 
    "name": "Antikt bord", 
    "description": "En fint antikt bord", 
    "startTime": "2016-12-10T10:00:00", 
    "endTime": "2017-03-29T10:00:00", 
    "imageUrl": "http://precisensan.com/antikforum/attachment.php?attachmentid=78277&stc=1&d=1292057973", 
    "categoryId": 1, 
    "supplierId": 1, 
    "buyNowPrice": 18000, 
    "sold": false 
    } 
] 
+0

私はちょうどここに推測しているが、私はそれ '応答を疑います。 json()。array'が間違っている可能性があります。データが何であるかを確認する必要があります 到来。 '.array'を取り除くだけで、あなたのデータがどのように見えているかわからないほど簡単かもしれません.... – Alex

+0

あなたのケースでは' response.json() 'はデータを取得するのに十分だと思います – VadimB

答えて

-2

AuctionService:

getAuctions() : Observable<Auction[]> { 
     return this.http.get(this.auctionsUrl) 
      .map((response: Response) => response.json()) 
      .catch(this.handleError) 
    } 

AuctionComponent:

getAuctions(): void { 
    this.auctionService.getAuctions() 
    .subscribe(auctions => this.auctions = auctions); 
    } 
関連する問題