PhilipB PhilipB - 3 months ago 7
Javascript Question

Assigning variable with $.getJson

I am creating a chart with nvd3. Since i am pretty new to javascript i've got a little problem assigning the chartdata.
When i assign it like this:

var long_short_data = [
{
"key": "ISP",
"color": "#d62728",
"values": [
{
"label" : "oäop" ,
"value" : 9000
} ,...
]
},
{
"key": "Organization",
"color": "#d67548",
"values": [
{
"label" : "amklöericalököky" ,
"value" : 8000
},...
]
}
];


The graph is displayed as it should.
But when i assign it like this:

var long_short_data = $.getJSON( "/link/to/multiBarHorizontalData.json", function() {
console.log( "success" );
})


It doesnt.
These are my console logs:

success
nvd3.js:9229 Uncaught TypeError: data.map is not a function


Any help appreciated, thanks in advance

Answer

$.getJSON doesn't return result as you are expecting. It returns data in callback. It should be

$.getJSON( "/link/to/multiBarHorizontalData.json", function(result) {
   long_short_data = result;
});
Comments