Currently, I am working on a Cordova/Phonegap application that utilizes an internal database. Typically, after executing a query, I would retrieve and review the results as shown below:
for (var i=0;i<results.rows.length;i++)
{
name=(results.rows.item(i).name);
alert(name);
}
However, due to issues with the RANDOM()
SQLite function, as discussed in this thread, I decided to manually shuffle the results:
function shuffle(array) {
var counter = array.length, temp, index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
var resultArray = [];
for(var x=0; x < results.rows.length; x+=1) {
resultArray.push(results.rows.item(x));
}
var res = shuffle(resultArray);
for (var i=0;i<res.rows.length;i++){
name=(res.rows.item(i).name);
}
ERROR:
Uncaught TypeError: Cannot read property 'length' of undefined
I am currently encountering this error. Could you please explain why this is happening and provide guidance on how to resolve it? Thank you!