0

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

btf217
  • 51
  • 1
  • 2
  • 7

1 Answers1

0

Technique is found at this page How to get return value in a function with inside Ajax call - JQuery

This is the working code:

function getVillages(callback) {
         var data = [];
        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++) {
                    data.push(village[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
                }

                if (typeof callback === "function") callback(data);            
            }
       });
    }

function addVillages() {
    var data = getVillages(function(data){
        for (i=0; i < data.length; i++) {
            //do whatever you want with the data
        }
    });
}
Community
  • 1
  • 1
btf217
  • 51
  • 1
  • 2
  • 7