10-12-2012 02:28 PM - edited 10-12-2012 02:37 PM
My function returns undefined values. It seems like the function return statement is excecuted before database is queried and values are assigned to variables. if I do a alert within for() loop I get the correct values from the database, however these values are not assigned prior to the function excecuting the return. How can I do this differently?
function getColumnNames(table){
var rs1, rs2, rs3, rs4, rs5;
db.transaction(function (tx) {
tx.executeSql("SELECT * FROM schema WHERE table_name=?",[table], function(ax, results){
var size = results.rows.length, i;
var item;
for (i = 0; i < size; i++){
item = results.rows.item(i);
rs1 = item.col1;
rs2 = item.col2;
rs3 = item.col3;
rs4 = item.col4;
rs5 = item.col5;
alert(rs5); //correct values are being returned
}
});
});
return [rs1, rs2, rs3, rs4, rs5];
}
10-13-2012 10:53 AM
Hey barlowdot1,
Your return will trigger before the data is ready, because it is outside of db.transaction() and executeSql(). Please place you return function inside db.transcation and test.
function getColumnNames(table){
var rs1, rs2, rs3, rs4, rs5;
db.transaction(function (tx) {
tx.executeSql("SELECT * FROM schema WHERE table_name=?",[table], function(ax, results){
var size = results.rows.length, i;
var item;
for (i = 0; i < size; i++){
item = results.rows.item(i);
rs1 = item.col1;
rs2 = item.col2;
rs3 = item.col3;
rs4 = item.col4;
rs5 = item.col5;
alert(rs5); //correct values are being returned
}
return [rs1, rs2, rs3, rs4, rs5];
});
});
}