简体   繁体   中英

Array not returned properly(Titanium)

I am returning the array using a function in titanium.There are two entries in my array which are showing alert but when i access the returned value in another js file.It only show one value in alert Here is my code (it is in db.js):

function quizfun() {
    var dataArray=new Array();
    var quizes = db.execute('select * from Quiz');
    while (quizes.isValidRow()) {
        var counter = 0;
        dataArray[counter] = quizes.fieldByName('Quiz_Text');
        quizes.next();
        alert(dataArray[counter]);//Showing two values
        counter++;
    };
    return dataArray;
}

Here is my other js file:

  quizes = db.quizfun();
  alert(quizes[0]);//working
  alert(quizes[1]);//alert not showing anything

Could you tell me what i am doing wrong.Thanks in advance

var counter = 0; should be outside the while loop. Like

function quizfun() {
    var dataArray=new Array();
    var quizes = db.execute('select * from Quiz');
    var counter = 0;
    while (quizes.isValidRow()) {
        dataArray[counter] = quizes.fieldByName('Quiz_Text');
        quizes.next();
        alert(dataArray[counter]);//Showing two values
        counter++;
    };
    return dataArray;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM