错误:对象不是用于将数组返回给我的callback函数的函数
我正在从智能行读取表格值。我通过单击每一行读取每个值来读取表格。
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);
我遇到了问题return callback(uiArray);
console.log(uiArray); //this returns value successfully.however i am unable to return the array to my script.
你用两个参数调用getTableData()
,第二个参数是一个callback:
aLib.getTableData(client, function (uiTable){
但是,您可以使用一个参数来定义函数:
exports.getTableData = function(callback){
因此, 您正试图将对象client
作为一个函数调用 ,这是无法工作的。
您定义的参数应该与传递的参数匹配,例如:
exports.getTableData = function(client, callback){
看来,这个问题不是与uiArray,而是与callback。
错误表明在你的代码中的某个地方,你正在用一个对象而不是一个函数调用exports.getTableData
。
然后当你尝试return callback(uiArray);
你得到错误。