Reputation: 115
My goal is to set properties like name, hidden and no_parent dynamically but it keeps giving me:
TypeError: Cannot set property 'name' of undefined
Even if I initialized scorcroot
before passing it by parameter.
Here is the code:
adattamento: function(data) {
var continua = true;
var scorcfat = this.famiglia;
var scorcroot = {};
this.controlloprimi(scorcroot, scorcfat);
this.root = scorcroot;
console.log("hey!")
console.log(this.root);
},
controlloprimi: function(scorcroot, scorcfat) {
scorcroot.name = scorcfat.name;
scorcroot.hidden = false;
scorcroot.no_parent = true;
if (scorcfat.father != null) {
if (scorcfat.mother != null) {
scorcroot.children = [{}, {}, {}];
this.controlloprimi(scorcroot.children[1], scorcfat.father);
scorcroot.children[2].name = "";
scorcroot.children[2].hidden = true;
scorcroot.children[2].no_parent = false;
this.controlloprimi(scorcroot.children[3], scorcfat.mother)
} else {
scorcroot.children = [{}]
this.controlloprimi(scorcroot.children[1], scorcfat.father);
}
}
if (scorcfat.mother != null) {
scorcroot.children = [{}, {}];
this.controlloprimi(scorcroot.children[1], scorcfat.mother);
}
},
Upvotes: 0
Views: 417
Reputation: 115
The problem was the wrong enumeration of the arrays.. So i created an array of 3 objects and by calling array[3] i was calling the 4th undefined object
Upvotes: 0
Reputation: 68393
Your problem seems to be this line
this.controlloprimi(scorcroot.children[3],scorcfat.mother)
since you have only given 3 items in this array initialization
scorcroot.children=[{},{},{}];
it means scorcroot.children[3]
is undefined
Not sure about the purpose of this code, so I would simply suggest making it 4 items instead of 3
scorcroot.children=[{},{},{},{}];
Upvotes: 1
Reputation: 1521
scorcroot.children[3]
is not an object as you have initialized the scorcroot.children
array with 3 objects only. So scorcroot.children[3] is undefined and you are setting property on undefined.
Upvotes: 1