0

I have a simple function to count the number of rows in a database and return the result. However I am getting the result returned as undefined, even though if I console.log the result it is coming up correctly from within the function. Here is the gist of what I am doing although I have stripped out a lot of code to simplify it.

$('#roomsList').append(getCount(currentRow.roomtype));

function getCount(roomtype) {
  var query = "SELECT COUNT(*) FROM fixturesfittings WHERE roomtype = ?;"
  localDatabase.transaction(function(trxn) {
    trxn.executeSql(query, [propertyid, roomtype], function(transaction, results) {
      return results.rows.item(0)["COUNT(*)"];
    }, errorHandler);
  });
}

Can anyone help me?

3
  • I suppose getCount() is async done by transaction Commented Jan 5, 2013 at 16:33
  • you have only roomtype in query but you are sending propertyid and roomtime for execution, could that be causing your sql to not return anything? Commented Jan 5, 2013 at 16:36
  • Thanks Hanky Panky although no, I just stripped that out of the code to simplify things, the other answers were correct in that the function is asynch.. Commented Jan 5, 2013 at 23:41

2 Answers 2

1

The problem is localDataBase.transaction and exequteSql are asynchronous functions. They won't have their answer right away, which is why you pass the functions into them. Once they gets an answer, they calls your function, known as a callback. This will happen at a later point in the execution cycle, after getCount is long gone.

So getCount calls localDatabase.transaction, which gets to work but doesn't have anything immediately available, so getCount finishes before the data is available, and so getCount is returning undefined (the default return value in JavaScript functions).

You will probably need to rework your code to something like this:

getCount(function(count) {
    $('#roomsList').append(count);
});

function getCount(callback) {
    var query = '...';
    localDatabase.transaction(function(trxn) {
        trxn.exequteSql(query, ...  function(transaction, results) {
            callback(results);
        });
    }
}

This is a very common pattern JavaScript, and has lots of pitfalls and oddities to it. It takes some getting used to.

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

Comments

0

There are two equally serious problems with what you're doing:

  1. Your outer function doesn't return anything. The only return statement in your code is returning from the inner function being passed to localDatabase.transaction.

    Something like this:

    function myFunction
      inner_function = function () {
        return 3; # Return from inner function, *not* myFunction
      }
    }
    
  2. Even if you were attempting to communicate a value out of your inner function to the outer function and then returning from the outer function, it still wouldn't work; the inner function is being invoked asynchronously. That inner return results... is being invoked at some point in the future, long after getCount has finished executing and returned undefined. You need to pass in some form of callback into getCount; the callback will be invoked later and passed the count. Think of it this way:

    function myFunc(callback) {
    
      function inner_function = function(callback) {
        // Invoke our callback sometime in the future
        setTimeout(1000, callback);
      }
    
      // Inner function does some processing at some future time;
      // give it a callback to invoke when its processing is done
      inner_function(callback);
    }
    

3 Comments

Thanks everyone, the asynchronisity of javascript still baffles the hell out of me - especially since I keep trying to make it behave synchronously. Javascript and Jquery is new to me and although I get the concept, it still all seems foreign to me! Thanks for all your help, it all makes sense now.
It seems like asynchronous development is supposed to make things quicker but actually slows things down...
It has nothing to do with making development quicker. JavaScript is single threaded. Asynchronous callbacks are meant to allow the browser to remain responsive rather than becoming completely frozen during every AJAX request. The alternative to async callbacks is actual threading, which (in terms of complexity) makes async callbacks very attractive.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.