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
Posts: 36
Registered: ‎02-06-2011
My Device: bold 9900
My Carrier: Vodacom

Function return undefined values

[ Edited ]

My function returns undefined values. It seems like the function 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. 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(rs5); //correct values are being returned
	   		}
	   	});	
	});
	return [rs1, rs2, rs3, rs4, rs5];
}

 

Highlighted
Retired
Posts: 165
Registered: ‎09-24-2012
My Device: Passport
My Carrier: Bell

Re: Function return undefined values

Hey barlowdot1,

 

Your return will trigger before the data is ready, because it is outside of db.transaction() and executeSql(). Please place you return function inside db.transcation and test.

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(rs5); //correct values are being returned
	   		}
	   		return [rs1, rs2, rs3, rs4, rs5];
	   	});	
	
	});
	
}