I am facing an issue with my JavaScript object that is retrieved from MySQL. The object has a property which contains an array of other objects, as demonstrated below:
parentObject = { ID: "1",
Desc: "A description",
childObjectArray : [ { item: "an item", cost: "cost" },
{ item: "another item", cost: "another cost" } ]
};
The problem arises when I pass the parentObject
to a function. While I can view the entire object, attempting to access parentObject.childObjectArray
returns undefined
. Strangely, I am able to access parentObject.ID
and parentObject.Desc
without any issues.
Could you please help me identify what mistake I might be making in this scenario?
Additional information and some sample code for reference:
Angular controller code snippet (tried with and without hasOwnProperty
, but no change in outcome):
this.doStuff = function() {
for (parentObject in this.parentObjects) {
if (this.parentObjects.hasOwnProperty(parentObject)) {
myService.doStuff(this.parentObjects[parentObject]);
}
}
};
Snippet from Angular service called by the controller:
function doStuff (obj) {
console.log(obj); // Object looks fine, both parent and child objects visible
console.log(obj.childObjectArray); // Returns 'undefined'
console.log(obj.childObjectArray[0]); // Still shows 'undefined'
};