Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
barlowdot1
Posts: 36
Registered: ‎02-06-2011
My Device: bold 9900
Accepted Solution

Function return undefined values

My function returns undefined values. It seems like the function's return statement is excecuted before database is queried and values are assigned to variables. if I do a alert within for() loop I get the correct values from the database, however these values are not assigned prior to the function excecuting the return statement. How can I do this differently?

function getColumnNames(table){
	var rs1, rs2, rs3, rs4, rs5;
	db.transaction(function (tx) {
		tx.executeSql("SELECT * FROM schema WHERE table_name=?",[table], function(ax, results){
			var size = results.rows.length, i;
			var item;
	   		for (i = 0; i < size; i++){
	   			item = results.rows.item(i);
	   			rs1 = item.col1;
	   			rs2 = item.col2;
	   			rs3 = item.col3;
	   			rs4 = item.col4;
	   			rs5 = item.col5;
                                alert(rs1); //correct value is returned here  			
	   		}
	   	});	
	});
	return [rs1, rs2, rs3, rs4, rs5];
}

 

 

 

Developer
jamesgibbons
Posts: 26
Registered: ‎06-01-2010
My Device: Dev Alpha, PlayBook

Re: Function return undefined values

 

Hi Fabian,

 

You are completely correct that the return statement is executing before the database returns the results. There are asynchronous calls going on. You can put breakpoints in the JavaScript to see what is going on. You can correct the problem by using function callbacks. I edited your code below to use callback functions - there might be some typos, but the general approach should be oaky.

 

function getColumnNames(table, callback){
	var rs1, rs2, rs3, rs4, rs5;
	db.transaction(function (tx) {
		tx.executeSql("SELECT * FROM schema WHERE table_name=?",[table], function(ax, results){
			var size = results.rows.length, i;
			var item;
	   		for (i = 0; i < size; i++){
	   			item = results.rows.item(i);
	   			rs1 = item.col1;
	   			rs2 = item.col2;
	   			rs3 = item.col3;
	   			rs4 = item.col4;
	   			rs5 = item.col5;
  			        alert(rs5); //correct values are being returned
  			    callback([rs1, rs2, rs3, rs4, rs5]);
	   		}
	   	});	
	});	
}


// example of how to call getColumnNames with callback
getColumnNames('importantTable', function (result)
{
	console.log(result);
});

// alternative version
function onCallback(result)
{
	console.log(result);
}

getColumnNames('importantTable', onCallback);

 

 

I hope this helps,

 

Cheers,

 

James

___________________________________________________________
My latest Blackberry 10 app Audiobooks! :smileyhappy:

My latest PlayBook app Photojam!

Find me on Twitter: GoGibbons
Developer
barlowdot1
Posts: 36
Registered: ‎02-06-2011
My Device: bold 9900

Re: Function return undefined values

Hi James.

 

Thanks your solution works perfectly. Much appreciated.