Reputation: 764
I'm stuck with the following and I'm sure it's do to my lack of experience dealing with callbacks, I want to query a db as follows:
db.transaction(queryFoodDBAll, errorCB);
function errorCB(tx, err) {
alert("Error processing SQL: "+err);
}
function queryFoodDBAll(tx) {
tx.executeSql('SELECT * FROM Food', [], displayAllFood, errorCB);
}
function displayAllFood(tx, results) {
var len = results.rows.length;
var items = new Array();
for (var i=0; i<len; i++){
items[i] = results.rows.item(i).data
}
printdata('settings-food',items, 'Food');
}
function printData(type,data,title) {
switch(type)
{
case 'settings-food':
alert("Data: "+data);
var content = '<div data-role="collapsible" data-theme="c"><h3>'+title+'</h3>';
for (var i=0;i<data.length;i++) {
content += '<p>'+data[i]+'</p>';
}
content += '</div>';
$("#settings-food").append(content);
break;
}
Now, when I run it, all the information is retrieved successfully until I run the printData function, which gives me the the errorCB displaying "Error processing SQL: undefined".
Any ideas on this?
Upvotes: 3
Views: 2437
Reputation: 5682
Your issue is the return stack:
When you get to printData your call stack looks like this:
----queryFoodDAll
----executeSQL
----disiplayAllFood (jump to printData)
----errorCB
----printData
Javascript will return up that call stack; so while you skipped errorCB
on your way down, on the way back up it hits that function, no matter what you do; because it's part of the callback stack.
What you want to do is not have your errorCB
as a call back function.
Instead check in your displayAllFood()
for valid SQL. If its valid continue down the call list (meaning continue onto printData
) but if its not valid then call errorCB
.
Upvotes: 2