return value nested in function

return value nested in function

Dear collective wisdom,
Disregarding the API calls to sqlite in the following code, I want to reference selectLocation as a variable that returns the JSON object for later manipulation. I realize I cannot return this within the nested db.transaction closure, which is why I used the variable jsonOut to grab the object after it was assembled. The call to console.log("In:" + JSON.stringify(json)) writes out the expected value, but unfortunately, when I try assigning selectLocation to a variable, I get that it is undefined. Am I barking up the wrong tree to get my object?


var selectLocation = function () {
var SelectStatement = 'SELECT * FROM site s  '
+ 'INNER JOIN evaluation e ON s.site_id = e.site_id '
+ 'ORDER BY s.site_id ';
db.transaction(function (transaction) {
transaction.executeSql(SelectStatement, [], function (transaction, result) {
if (result !== null && result.rows !== null) {
var i  = 0,
max,
row,
location,
complete,
notExists,
category,
jsonToEvaluate = []; // initialize as a collection 
for (i = 0, max = result.rows.length;  i < max; i += 1) {
row = result.rows.item(i);
location = row.address 
+ ' ' + row.city 
+ ' ' + row.state 
+ ' ' + row.zip;
complete = row.date_entered_on_device_by_evaluator;
notExists = row.no_longer_exists;
if (complete || notExists) {
category = "evaluationComplete";
} else {
category = "evaluationNotComplete";
}
jsonToEvaluate[i] = {
id: row.evaluation_id,
category: category,
location: location
};
if (i + 1 === max) {
jsonOut(jsonToEvaluate);
//alert(JSON.stringify(jsonToEvaluate));
//return jsonToEvaluate;
}
}
}
}, errorHandler);
}, errorHandler, nullHandler);
return;
}


// return  json object 
function  jsonOut (json) { 
console.log("In:" + json); 
return json;
}

// test underscore template

var test = selectLocation();
console.log("Out:" + test); 

Thanks in advance!

Greg--