Nothing is being return from my function because it is asynchronous. How do I return the viallages?
function getVillages() {
var self = this;
this.a = [];
VCare.VCareWebService.getFacilitiesForUser({cache:false,
callback:function(xml) { // invoke the service
// use jQuery to extract information we are interested in
console.log(xml);
var village = xml.getElementsByTagName("VCVillage");
for (i=0;i<village.length;i++) {
self.a.push(village[i].getElementsByTagName("description")[0].childNodes[0].nodeValue); // Uncaught TypeError: Cannot call method 'push' of undefined
console.log(village[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
}
}
});
console.log(self.a);
//return(allVillages);
}
New attempt at making a village object and giving it an allVillages attribute and updating it:
function Villages() {
this.allVillages = [];
}
function getVillages(village1) {
VCare.VCareWebService.getFacilitiesForUser({cache:false,
callback:function(xml, village1) { // invoke the service
// use jQuery to extract information we are interested in
console.log(xml);
var village = xml.getElementsByTagName("VCVillage");
for (i=0;i<village.length;i++) {
village1.allVillages.push(village[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
console.log(village[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
}
if(typeof callback === "function") callback(xml);
}
});
//return(allVillages);
}
function addVillages() {
village = new Villages();
console.log(village); //Villages {allVillages: Array[0]}
getVillages(village);
console.log(village.allVillages); //[]
for (i=0; i < data; i++) {
console.log(data[i]);
console.log(data[i][1]);
}
}
I get Uncaught TypeError: Cannot call method 'push' of undefined
as village1 seems to be undefined. But I do pass it a correct Villages object