3

I'm working on a database function and need to return results from fetchAll so I can use it elsewhere in my code but am not sure how to do it:

function fetchAll(sql,params,tableref){
  var fields = new Array();
  var resultout = new Array();

  for (i in tableref){     
    fields.push(i);  
  }       

  getResults(sql,params,fields,function(results){
     // I WANT TO RETURN RESULTS
     resultout.push(results);       
  });          

// TO HERE SO I CAN RETURN from Fetchall
console.log(resultout);
}

function getResults(query,params,fields,callBack){
  var result = new Array(); 
  thisDB.transaction(function (tx) {
    tx.executeSql(query,params, function(tx, rs){
       for(var i=0; i<rs.rows.length; i++) {
          var row = rs.rows.item(i);
          var rowresults = new Object();

          for (x=0;x<fields.length;x++){
            rowresults[fields[x]] = row[fields[x]];      
          }

          result.push(rowresults);
       }
       callBack(result);
    });
  }); 

return result;  
}

I think i'm missing something obvious.

Thanks

Antony

user1018244
  • 113
  • 6
  • 1
    You should place the console.log call inside the callback (below the resultout.push), if I'm understanding you correctly. – mkey Dec 20 '12 at 15:59
  • Can you please describe what exactly your problem is? What is this code supposed to do exactly and what does it do instead? – Philipp Dec 20 '12 at 15:59
  • 1
    The short answer is *don't* have `fetchAll` return a value. Instead, have `fetchAll` accept a callback that gets called in the callback of `getResults`. You can't have a function return a value synchronously when it relies on an asynchronous method to get that value. – apsillers Dec 20 '12 at 16:18

2 Answers2

0

If I'm understanding correctly your question, to see the result you need this

function fetchAll(sql,params,tableref){
    var fields = new Array();
    var resultout = new Array();

    for (i in tableref){     
        fields.push(i);  
    }       

    getResults(sql,params,fields,function(results){
        // I WANT TO RETURN RESULTS
        resultout.push(results);
        console.log(resultout);
    });
}

The callback will be executed "after" so basically in your example you'd see an empty result. This is because of the asynchronous nature of the callback.

mkey
  • 535
  • 2
  • 12
  • Wow - thanks for your responses. What I'm trying to do is basically get fetchAll to return an object of rows/fields from the database. If theres a better way to do it - please say. – user1018244 Dec 20 '12 at 16:26
  • From the top of my head, this looks OK, you just need to be aware of the asynchronous nature of your function. – mkey Dec 20 '12 at 16:43
0

I think you are trying to move from an asynchronous request to a synchronous one

Is a good topic, you can find many posts and solutions addressing this on the web

You have several options:

  • Use callback instead return in your function: Your function must receive another param (the callback) and call that callback passing to it the value that you want to return

    function fetchAll (sql, params, tableref, callback) {
      var fields = new Array();
    
      for (i in tableref) {     
        fields.push(i);  
      }       
    
      getResults(sql, params, fields, function (results) {
         // I WANT TO RETURN RESULTS
         callback(results);       
      });
    }
    

    Then you can log the results like this:

    fetchAll(sql, params, tableref, function (results) { console.log(results); });
    
  • Find a synchronous version of getResults, often there is one function for that, maybe getResultsSync ?

  • With node you can use https://github.com/laverdet/node-fibers or http://github.com/maxtaco/tamejs for converting from asynchronous to synchronous style (I'm using tamejs for that purpose currently and it is great!)
  • And others I don't know enough to talk about
Pau Fracés
  • 1,077
  • 10
  • 22