1

I am actually trying to retrieve something for a sqlite database and store the results in an array. So I create an array and pass it as an argument to the function. Then after the function returns, the array remains undefined. What is the problem with that?

function initGraphView()
{
   var resultsArray=new Array(12);
   getGraphData(defaultQueryString,resultsArray);
   alert(resultsArray[0]); //undefined
}
    getGraphData=function(queryString,resultsArray)
{
dbConnection.transaction(function(tx){
 tx.executeSql(queryString, [],
    function(SQLTransaction, data)
    {
        for(var i=0;i
3
  • please post your full getGraphData function and also usually arrays are initialized like var resultsArray = []; Commented Apr 2, 2011 at 4:50
  • @Erik, yes its running on Google chrome Commented Apr 2, 2011 at 4:54
  • @errorhandler it is the entire function Commented Apr 2, 2011 at 4:54

4 Answers 4

1

I don't really know what the getGraphData function is doing, but it looks like it's asynchronous. See the function passed into dbConnection.transaction? That's probably a callback that executes once the operation is complete, at some later time. You will need to handle the result from there instead.

Also, the function is passed the array as parameter 'anArray', but you're not using that parameter, you're using 'resultsArray'. Since that isn't defined in that scope, it's not the same array you think.

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

Comments

1

I don't know javascript well but there is something called passByValue and PassByReference... maybe try setting what you return, something like:

    vatAdded=addVAT(orderTotal)

    function addVAT(value){
      var newValue
      newValue=value*1.07
      return newValue
    }

HTH

Comments

1

you are not assigning the return value of function getGraphData to any variable.

resultsArray = getGraphData(defaultQueryString,resultsArray);

function initGraphView()
{
   var resultsArray=new Array(12);
   resultsArray= getGraphData(defaultQueryString,resultsArray);
   alert(resultsArray[0]); // undefined.
}




getGraphData=function(queryString,anArray)
{
dbConnection.transaction(function(tx){
 tx.executeSql(queryString, [],
    function(SQLTransaction, data)
    {
      for(var i=0;i<data.rows.length;i++)
      {
        var row = data.rows.item(i);
        var aName = row[name];
        var aMonth=row[month];
        var total=row[totalAmount];
        var aCategoryName=row[categoryName];
        anArray[parseInt(aMonth)]=parseFloat(total);
      }
    }
  )
});

return anArray;  // use return statement here
}

2 Comments

tried this out, it doesn't work. I guess its because of the asynchronous behavior in which the request is processed.
ur answer is right in the normal situation, but since i was using this script to fetch a data from a database, the call to it was asynchronous and thus the array came out to be undefined.
0

resultsArray from initGraphView is not visible in the getGraphData function object.

You need to change anArray to resultsArray.

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.