0

I'm trying to retrieve data from a db, to be precise a count, that I want to return from the function but it keeps returning me "Undefined". I'm using phonegap!

database.prototype.getNumberOfActions = function () {
    var numAct;
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
    db.transaction (function(tx){ 
            tx.executeSql('SELECT COUNT(*) AS conta FROM DB WHERE (((id >=' +idI + ') AND (anno >=' +annoI+')) OR (anno >'+ annoI+ '))) AND (fatto = true)',[], function(tx,results){numAct = results.rows.item(0).conta;},null); },null); return numAct;};`
dertkw
  • 7,798
  • 5
  • 37
  • 45
user1868607
  • 357
  • 4
  • 11
  • 1
    the `function(tx,results)` is a Asynchronous callback. You cannot return from an Async call. `return numAct` will not work. Have a look over [here](http://stackoverflow.com/questions/3903155/synchronous-query-to-web-sql-database#) for returning/using values from an Async call. – frank Aug 10 '14 at 14:33

1 Answers1

0

A list of thing to do:

  1. Make sure you can get anything from the database
  2. Make sure when you run the current JavaScript code you don't get an error. Using Chrome you can press F12 and press console.
  3. Use JSLint to check all of the code for problems, unfortunately you can check for problems without all the code (can someone verify this?)
  4. I have never used Phone Gap before, have you done something with it before to make sure it works?

Once you've done this, can you shed any light on the solution? To help people to come.

Alexander Craggs
  • 7,874
  • 4
  • 24
  • 46
  • Hi, this is not the first query i execute, I have already worked with javascript queries and it worked, but with the other queries i directly modified the application GUI so I had no problems with the Asynchronous Queries. I did everything that you said, the console doesn't display any problems and it already worked like I said before. What I'm trying to do is return the result of the query, but from what I understood, the return gets executed before the query, so the variable is set to undefined and doesn't display the query results. – user1868607 Aug 10 '14 at 14:32
  • Is it asynchronous? Have you tried getting the return to wait with "setTimeout()"? – Alexander Craggs Aug 10 '14 at 14:36