Reputation: 161
I am trying to loop through and lookup an id of an object to get the property of it and it's throwing: Uncaught TypeError: Cannot read property 'label' of undefined
If I try to output:
<%- fieldInfo["firstName"].label %>
it works.
Here is my code that is throwing the error:
fields = "firstName, lastName";
fieldInfo = { "firstName": [
{
"label": "Test Label",
"name": "Test"
}
] };
<% _.each( fields.split(","), function(field){ %>
<%- fieldInfo[field].label %>:
<% }); %>
I can for loop in js and it works as well but I am just learning underscore and trying to use this instead.
Upvotes: 0
Views: 26
Reputation: 2127
Your fieldInfo
is an object whose only property is an array of objects, you seem to want to access fieldInfo[field][0].label
.
Have you gone into your browsers developer tools and stepped through to see what values are present on each step along the way? It will make the problem (which boils down to the schema of your data) a lot clearer.
Upvotes: 1