0

Here is a very easy one I guess (but JS is not my best friend and I'm stuck...)

How can I return the rows value ? (which is undefined...)

function myFunction(table){
    html5sql.process(
    ["SELECT * FROM "+table],
    function(transaction, results, rowsArray){
        rows = rowsArray; // rows is defined here
    },
    function(error, statement){
    });
    return rows; // rows is not defined here :(
}
1
  • As the first line of your function: var rows;. The way it is now, rows is defined only within the scope of the inner function, so once it gets back out to where you're trying to return it, it doesn't exist anymore. By declaring it before you assign anything to it, you're ensuring that the variable named rows that is having values assigned to it is the one that can be returned. Commented Mar 11, 2013 at 17:40

3 Answers 3

4

You're probably not going to be able to do that as you have things now. With JS being by default asynchronous, rows will be returned even before any of those callbacks run. Since the success callback is what sets rows, you'll always be returning an either unset or stale value.

(Note: i have never used html5sql. It might be that the library just presents an interface that looks async while somehow actually working synchronously. But if it does, it'd be quite unusual in that regard.)

One possible fix would be to take a callback yourself, that you call and pass the rows to once you get them.

function myFunction(callback){
    html5sql.process(
    ["SELECT * FROM "+table],
    function(transaction, results, rowsArray){
        callback(rowsArray);
    },
    function(error, statement){
    });
}

// and use it like this
myFunction(function(rows) {
    // do stuff with rows in here
});

Or, just use callback as the second arg to process, if you want to be lazy. Just know that it will be passing all three args, which the caller of this function shouldn't have to care about. :P

Sign up to request clarification or add additional context in comments.

Comments

0

Declare rows before assigning it's value:

function myFunction(){
    var rows;
    html5sql.process(
    ["SELECT * FROM "+table],
    function(transaction, results, rowsArray){
        rows = rowsArray; // rows is defined here
    },
    function(error, statement){
    });
    return rows; // rows is not defined here :(
}

That changes the variable scope, and makes it visible and accessible outside inner function as well.

Comments

0

minor change...

function myFunction(){
    var rows;

    html5sql.process(
    ["SELECT * FROM "+table],
    function(transaction, results, rowsArray){
        rows = rowsArray;
    },
    function(error, statement){
    });
    return rows;
}

You're getting an undefined because you havent really defined it it - doing so at the beginning of the function will make it specific to that scope and should return properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.