0

So I'm using nightwatch for this and can't seem to push elements to an array that is inside a callback. It's showing "undefined".

            var tab = [];

            exports.command = function (control,tabs)
            {
                var self=this;
                var split = tabs.split("~"); //DELIMIT "tabs" PARAMETER

                self.elements('css selector', control+" :first-child", function (result) //COUNT THE CHILD OF PARENT
                {
                    var x = result.value;
                    var screenTabLen = x.length; //GET THE LENGTH OF ACTUAL TABS IN THE SCREEN
                    var tabLen = split.length; //GET THE LENGTH OF DELIMITED "tabs"     

                    function getTabText(index)
                    {
                        self.getText(control + " > :nth-child(" + index + ")", function(result){
                            tab.push(result.value);             
                        });

                    }

                    for (i=1; i<screenTabLen; i++)
                    {
                        getTabText(i);
                    }

                    console.log("TAB >> " + tab[1]);

                });

            };

how do I resolve this? thanks in advance! EDIT: Pasting the whole code

Yo Dawgy Dawg
  • 70
  • 1
  • 9

2 Answers2

0

Change tab.push() to bat.push() or change all bat to tab

var bat = []; // global variable

function getTabText(index) {
    self.getText(control + " > :nth-child(" + index + ")", function(result) {
        bat.push(result.value); // push values to array          
    });    
}

for (i=1; i<screenTabLen; i++) {
    getTabText(i); // loop function 
}

console.log("TAB === " + bat[1]); // getting undefined here
klefevre
  • 8,595
  • 7
  • 42
  • 71
AthMav
  • 196
  • 1
  • 8
0

You just made a typo :) you are trying to push to tab. But the array you defined is called bat.

var bat = []; //global variable
//here is your array called bat. 
            function getTabText(index)
            {
                self.getText(control + " > :nth-child(" + index + ")", function(result){
                    bat.push(result.value); //push values to array 
                    //change the tab to bat and you are good.


                });

            }

            for (i=1; i<screenTabLen; i++)
            {
                getTabText(i); //loop function 
            }

            console.log("TAB === " + bat[1]); //getting undefined here
FutureCake
  • 2,614
  • 3
  • 27
  • 70