2017-04-18 1 views
2

Am benutze angular2 mit Django Rest Api. mit http get in einem Dienst im Code versuche ein Konto abzurufen Gebrüll:Django Rest angular2 http bekommen

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

Sie versuchen, wann diese Methode in der Komponente zu beheben:

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

bekam ich eine Antwort mit http ok, aber das Konto in der Komponente ist nicht definiert: enter image description here

Antwort

3

Sie sind ein return fehlt, und Ihre Antwort scheint nicht data zu haben, so

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

sein sollte:

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

Ja effektiv vermisste ich eine Rückkehr danken u so much – sila