2017-04-18 33 views
2

上午使用angular2與Django rest Api。我試圖在波紋管的代碼服務來檢索使用HTTP GET一筆賬:Django Rest angular2 http獲取

getAccountBackend(id: string) : Promise<any> { 
    const slash="/" 
    const urlaccount = `${this.urlAccontBackend}${id}${slash}`; 
    console.log('url du compte', urlaccount) 
    return this.http.get(urlaccount) 
     .toPromise() 
     .then(response => { 
     response.json().data 
     console.log(response)} 
    ) 
    .catch(this.handleError); 
} 

當嘗試在組件解決這個方法:

getAccountById(){ 
    console.log("i will resolve the account"); 
    this.fcbAccSrv.getAccountBackend('1234896') 
    .then(data=> { 
     this.compte=data 
     console.log("the account from the backend", this.compte); 
    }) 
.catch(error =>{ this.error = error; 
    console.log('error to resolve account')}); 
} 

我以http確定,但響應帳戶組件是不確定的: enter image description here

回答

3

你缺少一個return,和你的反應似乎並不具有data,所以

.then(response => { response.json().data }) 

應該是:

.then(response => { return response.json() }) 
+1

是有效的我錯過了回報感謝ü這麼多 – sila