I'm a bit tired and I can't see why I'm getting undefined
errors on the variable cat.data[0].id
.
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
function Trial() {
var index = 0;
var cat = [];
function getcat() {
.
.Makes a call to get JSON set of data in the form
.{"status" : "success", "data" : [{"id":"8", "url":"http:\/\/google.com", "description":"Search Engine"}]}
.
request.success(function (data) {
cat = $.parseJSON(data);
if (cat.status === "error") {
alert(cat.message);
}
console.log(cat.data[index].id);
alert(cat.data[0].id);
});
request.error(function (data, status) {
alert("Error connecting to API:" + status);
});
}
}
function tests() {
getcat();
console.log(cat.data[index].id);
}
So when we call tests function it pulls the data and puts into array and the entries for console.log
and alert do exactly as they should and display the id field value. However when it returns to tests and does the console.log
entry there it fails with:
"Unable to get property '0' of undefined or null reference"
It's a global variable in the master function so why isn't it able to access it?