Reputation: 1601
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
Upvotes: 1
Views: 595
Reputation: 28775
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
}
Upvotes: 1
Reputation: 316
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
Upvotes: 1
Reputation: 8942
resultsArray
from initGraphView
is not visible in the getGraphData
function object.
You need to change anArray
to resultsArray
.
Upvotes: 0
Reputation: 14629
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.
Upvotes: 1