Hey all I’ve just been fooling around with the HTML5 Database API and I’m having trouble return the result from a function. Here is what I have.
var list = GetScenarios();
GetScenarios: function()
{
var scenarios;
// get all the scenarios
conn.transaction(function(_t) {
_t.executeSql('SELECT * FROM scenarios', [], function(_tr, _result) {
scenarios = _result.rows;
});
});
return scenarios;
}
I manage to get the rows from the db but the rows wont assign to the variable scenarios. I know its a scope thing but I can’t think how to return the result from the GetScenatios function.
When I log _result.rows I have the data there but scenario is always undefined;
Any thoughts?
Here is how my js file is setup.
window.MyExtension = (function()
{
return ({
OtherMethod: function() {
var list = window.MyExtension.GetScenarios();
},
GetScenarios: function() {
...see above
}
}());
It looks like you are trying to return code from an asynchronous request as though it were synchronous. You need to add a callback parameter to
GetScenarios():And change your calling code from:
To, this: