-1
function GetMe(id) {

DB.transaction(
    function (transaction) {

        transaction.executeSql("SELECT * FROM users WHERE id = ?", [id], function (transaction, results) {

            if(results.rows.length > 0) {

                var row = results.rows.item(0);

                return row.name;


            }

        }, errorHandler);
    }
);

}

alert(GetMe(1)); // id 5 exists in users table and alert shows empty

the above code dosent work so i tried the below codes

function GetMe(id) {

var retval;

DB.transaction(
    function (transaction) {

        transaction.executeSql("SELECT * FROM users WHERE id = ?", [id], function (transaction, results) {

            if(results.rows.length > 0) {

                var row = results.rows.item(0);

                retval = row.name;
                //alert('IN ' + retval); //works

            }

        }, errorHandler);
    }
);

//alert('OUT ' + retval); // undefined

return retval;  // undefined

}

alert(GetMe(1));  // undefined

the above code should work fine but it dosent i dont know whats wrong with it , i tried almost everything

can someone tell me whats wrong and help me fix this thanks :)

Pushpak
  • 161
  • 2
  • 8

2 Answers2

1

This looks like it's because DB.transaction is asynchronous. What you'll need to do is pass in a callback function to your GetMe() function, e.g.

GetMe(1, function (data) {

});

And then modify GetMe as follows:

function GetMe(id, callback) {
    DB.transaction(function (transaction) {
        transaction.executeSql("SELECT * FROM users WHERE id = ?", [id], function (transaction, results) {
            if(results.rows.length > 0) {
                var row = results.rows.item(0);
                callback.call(null, row.name);
            }
        }, errorHandler);
    });
}
jabclab
  • 14,786
  • 5
  • 54
  • 51
0

The problem is not the nested function, it is the fact that the inner function (the one passed into transaction.executeSql) does not execute when you think it does. The sequence is more like the following:

  1. GetMe runs
  2. DB.transaction is called and a callback passed in
  3. DB.transaction returns
  4. GetMe returns (nothing)
  5. The first callback is executed at some unknown later point
  6. executeSql is then called, and another callback passed in
  7. executeSQL returns
  8. the callback returns (nothing)
  9. The second callback then executes (once the SQL has run), and returns row.name, but to nothing, the rest of the program having log ago moved on.

Tl;dr: You should call another declared function rather than passing in a callback:

// inside GetMe
transaction.executeSql("SELECT * FROM users WHERE id = ?", [id], GotMe);

// outside, in the same scope as GetMe
function GotMe(transaction, results) {
    // do your stuff here
}
beeglebug
  • 3,522
  • 1
  • 23
  • 39
  • `function UpdateMe() { var data = GetMe(); DB.transaction( //more transactions and queries using the variable data ); }` @beeglebug this is what im trying to do get the data from a function `GetMe()` and then use it in another function for other updates the thing is ill be using `GetMe()` like 20 times all over the scripts and i dont want to repeat stuff need to keep it small and flexible. so your `GotMe()` is not a solution i think cause ill be using the data for various purposes or is there a way to get data from `GotMe()` maybe to all my other functions.. – Pushpak Jan 24 '12 at 14:50