1

Am using angular2 with django rest Api. Am trying to retrieve an account using http get in a service in the code bellow :

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

When try to resolve this method in the component :

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

I got a response with http ok but the account in the component is undefined : enter image description here

1 Answer 1

3

You are missing a return, and your response doesn't seem to have data, so

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

should be:

.then(response => { return response.json() })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.