2017-02-02 1 views
-2

Ich versuche, ein JSON-Array von einem API zu erhalten und es auf der Seite anzuzeigen, ich bin ziemlich sicher, dass etwas mit meinem Service nicht stimmt, ich weiß nur nicht was.Erhalten json Array von api in angular2

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

hier ist eine Antwort Beispiel

[ 
    { 
    "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

ich hier bin nur raten, aber ich vermute, dass 'Antwort. json(). array' könnte falsch sein, Sie müssen überprüfen, welche Daten sind Kommen. Es kann so einfach sein, '.array' zu entfernen, aber nicht zu wissen, wie Ihre Daten aussehen, als wäre es schwer zu sagen .... – Alex

+0

Ich denke' response.json() 'in Ihrem Fall ist genug, um Daten zu bekommen – VadimB

Antwort

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