I'm trying to add data to a global array variable in my ready function. When i later try to use the data my array is empty.
This is my ready function:
var counter = 0;
var services = [];
var names = []
var serviceData = []
$(document).ready(function () {
$.getJSON(JSurl + "/Home/getServicesForCustomer?name=" + '@Model', function (data) {
$.each(data, function (index, value) {
console.log("Trying to push services : " + JSON.parse(value.jsonTEMPLATE).komponent);
services.push(JSON.parse(value.jsonTEMPLATE).komponent);
console.log("After push : " + services);
names.push(value.Name);
serviceData.push(value.jsonDATA);
});
});
drawKomp(0);
});
This is the function where i want to use my data.
function drawKomp(index) {
console.log("Services in draw" + services);
});
This is the log
Services in draw NewCustomerWizard?name=Testkund:104
Uncaught TypeError: Cannot read property 'length' of undefined jquery-2.0.3.js:564
Trying to push services : [object Object],[object Object] NewCustomerWizard?name=Testkund:90
After push : [object Object],[object Object] NewCustomerWizard?name=Testkund:92
Trying to push services : [object Object],[object Object],[object Object] NewCustomerWizard?name=Testkund:90
After push : [object Object],[object Object],[object Object],[object Object],[object Object]
Why am i getting Cannot read? Seems like my data is added to the services array