错误:对象不是将数组返回到回调函数的函数

时间:2016-09-02 13:33:54

标签: javascript arrays node.js webdriver-io

我正在从UI行中读取表值。我正在通过单击每一行并读取每个值来读取表。

exports.getTableData = function(callback){
    var uiArray =[];
    var count;
   
    aLib.loadCheck(client);    
    //Clicks the first record of the table
    aLib.controlClick(client, obj.table.firstRecord);
    aLib.loadCheck(client);    
    // Gets the text of the total number of records on the top left of the table and uses it to drive the loop
    client.getText(obj.topContent.recordCount, function (err, rowNum){
       console.log(rowNum);
       count = rowNum.match(/\d/g).join("");
        console.log('No. of records on UI:', count);
          // Recursive function which clicks, reads the text in the selected row, and then clicks the next immediate row.
          // This way, the dynamic nature of the records appearing in the DOM, as one scrolls down is handled.
           function forLoop(i){
            client.getText(obj.table.selectedRow, function (err, text){

                var str = text.toString();//coverted UI text to string
                str = str.replace(/\n/g,",");//converted next line(\n/g) to comma
                str = str.replace(/\s/g, '');//removed blank space(\s/g)
                var text = str.split(',');//converted string to array again
                removed = text.splice(1,1);//  removed updated  by from UI
                removed = text.splice(6,1);// removed inserted on from UI
                removed = text.splice(7,2);// removed created by id from UI
               // console.log(text.length);
                uiArray.push.apply(uiArray, text);
               // console.log('UI array ki length',uiArray.length);
               console.log('i ki value :',i)
                if(i==count){
                     
                   console.log('inside if..............'); 
                     console.log(uiArray); 
                     return callback(uiArray);
                }
                client.click(obj.table.nextRow);
                forLoop(i+1);
                });


            }
         forLoop(1);
    }); 

};

我的脚本调用此函数getTableData。             试试{

               aLib.getTableData(client, function (uiTable){
                console.log('suman00');
                console.log(uiTable);    
                 });  

             client.pause(12000);
        } catch (e) {
            expect(false).toBe(true);
            throw new Error('testcase case failed because of exception : ' + e);
        }
        client.call(done);
    },250000);

我在return callback(uiArray);

处遇到问题
console.log(uiArray);  //this returns value successfully.however i am unable to return the array to my script.

2 个答案:

答案 0 :(得分:2)

您正在使用两个参数调用ref,第二个参数是回调:

getTableData()

但是,您可以使用一个参数定义函数:

aLib.getTableData(client, function (uiTable){

因此,您试图将对象exports.getTableData = function(callback){ 称为函数,这无效。

您定义的参数应与传递的参数匹配,例如:

client

答案 1 :(得分:0)

似乎问题不在于uiArray,而在于回调。

错误表示您在代码中的某个位置使用对象而不是函数调用exports.getTableData

然后,当您尝试return callback(uiArray);时,您会收到错误。