My Code is in phonegap application. While executing a SELECT SQL statement, I am experiencing difficulties to pass the results to parent function’s variable. The code is represented below:
function db_data(query) {
var result_out;
db.transaction(function (tx) {
tx.executeSql(query, [], function (tx, results) {
console.log("RETURNED SUCCESSFUL RESULTS"); // SUCCESSFULLY EXECUTING HERE.
result_out = results.rows;
}, function (){
console.log("Error on executing sql");
result_out = false;
});
});
console.log(result_out); // NOTHING LOGGING HERE.
return result_out;
}
This function is to pass common SELECT statements. The function is not returning any thing and the returning object is successfully logged only within the SQL execution function.
The operation is asynchronous. The operation inside
db.transactionmay take effect in a later time. By the time you logged the result, it isn’t there yet.If you want to execute something after getting a value for
result_out, you need to put it inside the callbacks:when using db_data, instead of
do this instead