1

JSON:

var promiseObj = {
"physical": [],
"virtual": []
}
var config = {
"Environments": [
"LH5",
"LH8",
"AMS"
],
 "Clusters": {
    "LH5": 4,
    "LH8": 4,
    "AMS": 4
}
};

So I am trying to set different promise objects based on the environments and the clusters in the given JSON object above.

for (var i = 0; i < config.Environments.length; i++) { 
    promiseObj.physical[config.Environments[i]][config.Clusters[config.Environments[i]]] = $http.get('URL').success(function(data) {
    //Successful stuff here 
});

}

However when performing this for loop I get the following error:

promiseObj.physical[config.Environments[i]] is undefined

Could someone shed some light into why this is returning undefined, when the object is clearly defined at the start of the document?

7
  • 7
    promiseObj.physical = []; therefore promiseObj.physical[anything] will be undefined. It doesn't matter what config.Environments[i] returns because the physical array is empty. Commented Sep 1, 2015 at 14:56
  • So what is the solution? Commented Sep 1, 2015 at 14:57
  • 2
    Create those keys instead of only trying to access them. Commented Sep 1, 2015 at 14:58
  • Since config.Environments[i] is a string, promiseObj.physical[string] has wrong syntax (array index cannot be a string Commented Sep 1, 2015 at 14:59
  • I would first break that long expression into shorter expressions using local variables. Eg var envname = config.Environments[i]; instead of repeating that expression. It makes it easier to read. Then double-check that you really want the multiple levels of nesting in the data structure that the code expects (but currently fails because you can't index undefined). Commented Sep 1, 2015 at 15:00

3 Answers 3

1

Since physical is an empty array, any and all keys within it are undefined. You need to prime them the first time you use them:

if (typeof promiseObj.physical[config.Environments[i]] == 'undefined') {
    promiseObj.physical[config.Environments[i]] = {};  // or [], whichever you want
}
promiseObj.physical[config.Environments[i]][config.Clusters[config.Environments[i]]] = ...
Sign up to request clarification or add additional context in comments.

Comments

1

You're declaring physical as an array and then accessing it as an object, so you're trying to access a property that can't exist. Try declaring physical as follows:

"physical": {}

Comments

1

This is because promiseObj.physical[] is an empty array, you have to initialize the index you want before:

for (var i = 0; i < config.Environments.length; i++) { 
    if(!promiseObj.physical[config.Environments[i]]) promiseObj.physical[config.Environments[i]] = {}; // Initialize it
    promiseObj.physical[config.Environments[i]][config.Clusters[config.Environments[i]]] = $http.get('URL').success(function(data) {
    //Successful stuff here 
    });

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.