2017-04-18 71 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