3

I am pulling data from an API for recipes, I already have a list of recipes which works fine, however I am now calling for a single recipes details (1 object). My console log below is what the JSON looks like. Whatever I do I cannot get to display on the front end, please help where you can.

JSON

TypeScript

details: any;

loadDetails() {
  if (this.details) {
    return Promise.resolve(this.details);
  }

  return new Promise(resolve => {
    this.http.get('http://api.yummly.com/v1/api/recipe/Crustless-Ham-Savory-Bake-2005439?_app_id=//////&_app_key=/////')
      .map(res => res.json())
      .subscribe(data => {
        console.log(data);
        this.details = data;
        resolve(this.details);
      });
  });
}

HTML

<ion-content>
    <ion-list>
        <ion-item>
            <h1>{{details.id}}</h1>
        </ion-item>
    </ion-list>
</ion-content>

Pagename.ts

@Component({
  selector: 'page-details',
  templateUrl: 'details.html',
  providers: [ApiAuthentication]

})
export class DetailsPage {

  public api: any;

  constructor(public navCtrl: NavController, public navParams: NavParams, public apiAuthentication: ApiAuthentication) {
    this.loadRecipes();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad DetailsPage');
  }

  loadRecipes(){
    this.apiAuthentication.loadDetails()
    .then(data => {
      this.api = data;
    });
  } 
}
6
  • 1
    I dont see description anywhere in the returned object. you need a key to access try details.id forst to see if id is printing Commented Feb 13, 2017 at 17:33
  • @AniruddhaDas Sorry I have now changed to ID (was messing with other values) I get an error of Cannot read property 'id' of undefined Commented Feb 13, 2017 at 17:39
  • @BA1995. It's a classic case of async value: your template tries to access details.id before details is actually defined (it is NOT assigned immediately since it is the result of an async cold). Try {{details?.id}} in your template. Commented Feb 13, 2017 at 17:49
  • Use the elvis operator (?) like this to avoid that error <h1>{{details?.id}}</h1>. That's because the promise is not yet resolved when trying to print the id. Commented Feb 13, 2017 at 17:49
  • @AngularFrance Thankyou for this, I now have lost the error in console. As you can tell I'm new to this, advice on moving forward? Commented Feb 13, 2017 at 17:59

2 Answers 2

2

You are trying to display

<h1>{{details.id}}</h1>

when you have in fact your object in api:

loadRecipes(){
  this.apiAuthentication.loadDetails()
    .then(data => {
      this.api = data; // you store it in api!
  });

so this should probably be cleared out by just changing your template a bit:

<h1>{{api.id}}</h1>

and perhaps add the safe navigation operator here as well. {{api?.id}}

Sign up to request clarification or add additional context in comments.

2 Comments

Thats great! I didn't realise ? can get you through the error
The safe navigation operator guards null values, you can read more here: angular.io/docs/ts/latest/guide/… You'll get used to using this A LOT with Angular since we are constantly dealing with async :)
0

You are actually trying many thing and can be done with one of the tings.

you are using both promise and observable and you can avoid one of them. I would say use observable as it comes by default.

loadDetails() {
  if (this.details) {
    return null;
  }

  this.http.get('http://api.yummly.com/v1/api/recipe/Crustless-Ham-Savory-Bake-2005439?_app_id=//////&_app_key=/////')
      //.map(res => res.json())  // you don't need this
      .subscribe(data => {
        console.log(data);  // make sure the data is the object you are expecting and have the id property
        this.details = data; // not necessary if you will use async pipe
      });
  });
}

here details.id should be available.

1 Comment

Hi, I have edited my question to include the more code pulling that function, I do also need the .map(res => res.json()) to convert the json into an object

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.