我正在使用angular2和django rest Api。我试图在下面代码中使用http get在服务中检索一个帐户:
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 ok的响应,但是组件中的帐户没有定义:
发布于 2017-04-18 17:49:12
您缺少了一个return
,并且您的响应似乎没有data
,所以
.then(response => { response.json().data })
应:
.then(response => { return response.json() })
https://stackoverflow.com/questions/43477225
复制相似问题