NodeJs retrieving value from Array show as un

瘦欲@ 提交于 2019-12-25 01:55:33

问题


cellsData have array like below in global variable

 [   { id: 2,
    cellId: 'R2C3',
    row: '2',
    col: '3',
    value: '202',
    rawValue: '202.0',
    numValue: '$202' },
  { id: 3,
    cellId: 'R2C4',
    row: '2',
    col: '4',
    value: '2034',
    rawValue: '2034.0',
    numValue: '$2,034' }]

I have a soda script in which I want to verify the data after reading from this array problem is it gives error on cellsData[startRowId].numValue as, 27 undefined

I tried global.cellsData also but no working

error

.keyYears table tbody tr:nth-child('+i+') td:nth-child(3)',''+ cellsData[startRowI ^

TypeError: Cannot read property '27' of undefined

    var browser = soda.createClient({
    .....
    });

// Retrieving data from Excel sheet    
testsheet.setAuth( 'user', 'pass', function(err){

if (err) console.log(err);

testsheet.getCells( 1, function(err, cellsObj){

    if (err) console.log(err);   

    cellsData = cellsObj.cells;
    rowcount = cellsObj.RowCount;
});

})


browser 
    .chain
    .session()  
    .setSpeed(speed)
    .setTimeout(2000)
    .open('/')
    .and(login('dev@dev.com', 'x1212GQsdtpS'))
    .and(verifyData())
    .end(function(err){
        console.log('error');

    });

function login(user, pass) {
  return function(browser) {
    browser
    .click('css=a#loginButton')
    .type('css=input.input-medium.email',user)
    .type('css=input.input.pwd',pass)
    .clickAndWait('css=a.btn.login')
    .assertTextPresent('Clients',function(){ console.log('logged in ok')})
  }
}


function verifyData() {
            var startRowId=27
    console.log('inside TestingSpreaddsheet')
    var totalLoop=(rowcount-5)+1
  return function(browser) {
    browser
    for (var i = 1; i <= 4; i++) {
    browser.assertText('css=div.keyYears table tbody tr:nth-child('+i+') td:nth-child(3)',''+ cellsData[startRowId].numValue +'',function(){ console.log('looks good')})
    .assertText('css=div.keyYears table tbody tr:nth-child('+i+') td:nth-child(5)',''+ cellsData[startRowId].numValue +'')
    .assertText('css=div.keyYears table tbody tr:nth-child('+i+') td:nth-child(6)',''+ cellsData[startRowId].numValue +'')
    .assertText('css=div.keyYears table tbody tr:nth-child('+i+') td:nth-child(7)',''+ cellsData[startRowId].numValue +'')

    }
  }
}

UPDATE Soda is running the chain before fetching my ExcelSheet Data. How to control chain ?


回答1:


in node, most things are async, not sync. the data hasn't arrived yet hence rest process breaking.

the only place in code where I have the data is inside testsheet function so i moved my test code there and it fixed this issue.



来源:https://stackoverflow.com/questions/16261010/nodejs-retrieving-value-from-array-show-as-un

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!