0

Apologies if this is near a duplicate question, however all other question are either un answered or not quite as close to this issue.

I'm in Ionic 3 and I am using this code to get JSON from a feed:

getMenu(){

    let path = 'URL';
    let encodedPath = encodeURI(path);

    this.http.get(encodedPath)
    .map(res => res.json())
    .subscribe(data => {
      let responseData = data;
      if(responseData.status=='success'){
        var JSONObject = responseData.data;
        this.menuItems = JSONObject;
        console.log(JSONObject);
      }
    },
    err => {
        console.log('error ' + err);
    });
}

I call that function in both ionViewDidLoad and the constructor. And then in my page I'm trying to access the data in that feed on the page with:

{{ menuItems.length }} etc etc

I think i'm aware of the fact that the data will probably come in after the page has loaded as the error i'm getting is

Cannot read property 'length' of undefined

The console does show me the correct data that is pulled in, good. But as I have read, this will happen asynchronously. All the tutorials I have read say it just works, well it certainly doesn't!! Thanks in advance...

2
  • 1
    Try using {{ menuItems?.length }} - if it works then I'll tell you why Commented Jan 30, 2018 at 17:46
  • Ahhh, how VERY annoying!!! I had been trying with the question mark but putting it in the wrong place menuItems.length? Am I right in thinking that that sets the variable to be ready to look for a change in the HTML? Apologies in the question duplication, do I just remove the question? Commented Jan 30, 2018 at 17:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.