2017-04-18 20 views
2

Django rest APIでangular2を使用しています。試みは、コンポーネントでこのメソッドを解決するときにDjango Rest angular2 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); 
} 

:怒鳴るコードでサービスにHTTP GETを使用してアカウントを取得しようとしています

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

はい効果的に私は復帰を逃したが、そんなにuの感謝 – sila

関連する問題