Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm using Ionic to create an app, and in the controller, I'm getting an object that's resolved from the app.js. This object has several properties including an array like so:

{
    "objectId":"id",
    "objectName":"name",
    "anArray":[{
        "id":"id1",
        "name":"name1",
        "description":"Medium Rare",
        "photographUrl":"/image1"
    }, {
        "id":"id2",
        "name":"name2",
        "description":"Baked Beans on Scrumptious Dark Rye Bread",
        "photographUrl":"/image2"
    }],
    "emptyArray":[]
}

However, the problem is, I can iterate through each item in the array in the html using: (key, value) in theObject.anArray, but in the controller, I'm trying to iterate through the array in a for loop for (var i = 0; i < $scope.theObject.anArray.length; i++) {...}

However, I get an error:

TypeError: Cannot read property 'length' of undefined

Can anyone help me figure out why I cannot access this array in the controller, but I can in the html.

P.S. in the controller I use this:

$scope.theObject = theObject;

Thanks

share|improve this question
    
Can you do console.log($scope.theObject) ? – kawadhiya21 47 mins ago
    
Just returns [object Object] – Ramonster 37 mins ago
    
can we get more about that $scope.theObject? where is it coming from? – briosheje 32 mins ago
    
console.log(JSON.stringify($scope.theObject)).. Check the structure of object. See if the key exists or not. – kawadhiya21 19 mins ago
    
JSON.stringify() returns {}. The key does exist, and works in the HTML using ng-repeat="(key, value) in theObject.anArray The Object is being coming from a web request, a query on a $resource. The Object returned is correct as can be seen from the network activity. – Ramonster 6 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.