How do I call a method within a loop?

0

I'm trying to execute a function, which I have a call from another function inside a loop , however the return of the function only loads the first method entry. So, how do I call a method inside a loop ? Here is the code:

function 1:

componentesGrafico() {
    var i, j;
    var valores; 
    console.log(this.arraySlide[i]);
    for (i = 0; i < this.arraySlide.length;i++) {
        valores = this.db.queryDatabase(this.arraySlide[i]);
        console.log(valores); 
    }       

function 2:

queryDatabase(id) {
    var grupo = '/EMPRESA/COMPBI/';
    var db = this.afd.database.ref(grupo).orderByKey();
    var valores = new Array(); 
    db.startAt(id).endAt(id+"\uf8ff").on("child_added",function (snapshot) {
        valores.push(snapshot.key);
        console.log(snapshot.key);
    });
    return valores;
}
    
asked by anonymous 08.11.2017 / 19:57

1 answer

0

As Diego said: the 2nd function works asynchronously. When you return the array of values, it has not yet been initialized. You will have to put the body of function 2 in function 1:

componentesGrafico() {
    var i, j;
    var valores; 
    console.log(this.arraySlide[i]);
    for (i = 0; i < this.arraySlide.length;i++) {
        valores = new Array();
        var grupo = '/EMPRESA/COMPBI/';
        var db = this.afd.database.ref(grupo).orderByKey();
        db.startAt(id).endAt(id+"\uf8ff").on("child_added",function (snapshot) {
            valores.push(snapshot.key);
            console.log(snapshot.key);
        });
        console.log(valores); 
    }
}
    
24.02.2018 / 18:55