I am developing an application for iPhone but I write JavaScript. I store some information (the id and the name of an item) in the local database and now I need to get that information back so that I can use it. Unfortunately, the database returns to me undefined value but if I alert the value before the return statement, I get it correctly. My code is below:
function DB() {
myDatabase.transaction(function (transaction) {
transaction.executeSql("SELECT * FROM myTable WHERE id = ?", [idNum],
function (transaction, resultSet) {
var i = 0;
var currentRow = resultSet.rows.item(i);
return currentRow.name;
},
function (transaction, error) {
alert('error:' + error.message);
}
);
})
}
What am I doing wrong?
The transaction method you are using does not return a value. Instead it passes it to a result function. Returning a result from the result function does nothing. You have to use the result of the query within the result function.