user455318
user455318

Reputation: 3346

TypeError: json[i] is undefined

what is the reason of this error?

  <script type="text/javascript">
        dojo.xhrGet({
            url: "homeChart.php",
            handleAs: "json",
            load: function(json) {
                $m = [];
                for (var i = 1; i < 10; i++) {
                    $m.push(parseFloat(json[i]["valor" + i]));
                    alert(i);    
                }
                dojo.addOnLoad(makeCharts);
            }
        });
    </script>

the output of homeChart.php is exactly: [200]

thanks

Upvotes: 2

Views: 1937

Answers (2)

sitifensys
sitifensys

Reputation: 2034

You are doing : parseFloat(json[i]["valor" + i]

Even if i is in range, json[0]["valor0"] will be undefined, hens parseFloat returning NaN.

If your json is an array of floats, you should try something like

for (var i = 0; i < json.length; i++) {
    $m.push(json[i]);
}

Hope this will help.

Upvotes: 0

Zach
Zach

Reputation: 7930

json is [200], which has only one thing in it (200) at index 0.

The for loop uses indices 1 through 10 -- those indices aren't defined for this array.

Upvotes: 1

Related Questions