Reputation: 23
I loaded a json file with jquery and then tried to access certain properties but I get an undefined error. When I print the json object, I can clearly see the object in it.
{
"Calculus 1": {
"LEC": {
"sections": [
"LEC01",
"LEC02"
],
"days": [
[
"MO",
"WE"
],
[
"MO",
"TH"
]
],
"start": [
[
"12:00",
"11:00"
],
[
"13:00",
"13:00"
]
],
"end": [
[
"13:00",
"13:00"
],
[
"14:00",
"15:00"
]
]
},
"TUT": {
"sections": [],
"days": [],
"start": [],
"end": []
},
"PRA": {
"sections": [],
"days": [],
"start": [],
"end": []
}
}
}
Here is how a simplified version of my JSON looks like. The part in my code that tries to access a property looks like this.
if(info[courses[i]]['TUT']['sections'].length == 0){
tut = -1;
}
Here is how I loaded my JSON. The code above is inside the some function. When I print the info inside the function after passing it, it's exactly what I have in my JSON.
$.getJSON('/public/courseinfo.json', function(info){
var x = some_function(courses, info);
});
The error I get is "Cannot read property 'TUT' of undefined". Info is just the variable for the object I loaded from my JSON. When I print the variable to console, I get exactly how it should look like. If I just do `info[courses[i]] = some property' I don't get an error. 'info[courses[i]].length == 0' also prompts a cannot read property 'length' error. I'm not sure if it's a syntax issue, but I can't seem to figure out what is wrong with it.
Upvotes: 0
Views: 56
Reputation: 432
Error is throwing because info[courses[i]]
is undefined
. check whats returning as courses[i]
. courses[i]
should be returning Calculus 1
Upvotes: 1