Ask away

JSON component acess error

Larry 05/19/17 17:08


Question CategoryComponent,Server,Data Binding

Number of completed projects0

Question StatusUnsolved

  • JSON
  • Data interface
  • error


I've recently been working on the NEST application using JSON to create a dynamic feel. I have however recently hit a bit of a brick wall... The following function is used to parse some delivery data (irrelevant) into jquery mobile: function parseProdData(results) { var html = ''; for (day in results.deliveries) { var today = results.deliveries[day].delivery; var today_date_arr ='-'); var today_date = today_date_arr[2]+'/'+today_date_arr[1]+'/'+today_date_arr[0]; html += '<li><a href="#">'; html += today.delivery_day+', '+today_date; html += '</a></li>'; console.log(html); } $('#JSON-list').append(html); $('#JSON-list').trigger('create'); $('#JSON-list').listview('refresh'); } Now all this looks like its working fine as when I check the console log I get: <li><a href="#">Thursday, 27/02/2014</a></li><li><a href="#">Friday, 28/02/2014</a></li><li><a href="#">Monday, 03/03/2014</a></li><li><a href="#">Tuesday, 04/03/2014</a></li><li><a href="#">Wednesday, 05/03/2014</a></li><li><a href="#">Thursday, 06/03/2014</a></li><li><a href="#">Friday, 07/03/2014</a></li> Thus showing that it is accessing both the date and time attribute correctly. However, straight after this, I get an uncaught type error: Uncaught TypeError: Cannot read property 'date' of undefined From my understanding of JS this should only happen when the relevant attribute is unset. As we can see from the HTML output in the console, this is not the case as it is being accessed correctly. Finally, I get exactly the same error (with delivery_day as the 'undefined' attribute) if I restrict the code to just the delivery day. For those who would like it, below is a sample of the JSON code used: { "deliveries":[ { "delivery":{ "delivery_day":"Thursday", "date":"2017-02-27" } }, { "delivery":{ "delivery_day":"Friday", "date":"2017-02-28" } } ] } Does anyone have any idea why this error is popping up?


Ken 05/20/17 15:28

What happens if you use today['delivery_day'] instead?

Anton 05/21/17 05:35

Running your code in the console doesn't produce any errors for provided JSON object. Are you sure this has to do with this part of the code and not one of these functions: $('#JSON-list').trigger('create'); $('#JSON-list').listview('refresh');?

Terry 05/24/17 11:13

Either some of your JSON entries do not contain the date field (but that wouldn't populate your table), or there are additional triggers attached somewhere that cause the issue. Could you please comment the trigger lines and try only with appending the html for now?

Mike 05/24/17 21:32

I saved json data in result.json file then used this $.getJSON('result.json', function(result, status){ var today = result.deliveries; var html = ""; $.each(today, function(key, value){ $.each(value, function(key, value){ var today_date_arr ='-'); var today_date = today_date_arr[2]+'/'+today_date_arr[1]+'/'+today_date_arr[0]; html += '<li><a href="#">'+value.delivery_day+', '+today_date+'</a></li>'; }) }) $('#JSON-list').append(html); $('#JSON-list').trigger('create'); $('#JSON-list').listview('refresh'); }) .success(function(result) {}) .fail(function(jqXHR, textStatus, errorThrown) { }) .complete(function() { });

Douglas 05/26/17 08:59

The issue was just that there was an empty element at the end of the 'deliveries' block, this was causing the uncaught error. I didn't notice it because the element was empty.