MySQL in a separate function

All things HTML5 or text based engines, or really any web based engines.
Post Reply
alex_king92
Posts: 12
Joined: Fri Aug 30, 2013 9:21 am

MySQL in a separate function

Post by alex_king92 »

Hi, I need to make a function which query my MySQL db.
I've made this:
call:

Code: Select all

var data = {};
data = self.queryMySQL('SELECT * FROM table_one');
console.log(data);   //No output here
function:

Code: Select all

queryMySQL: function(query){
	var result = {};
	//connect to the mysql server
		ige.mysql.connect(function (err, db) {
			// Check if we connected to mysql correctly
			if (!err) {
				// Query the database
				ige.mysql.query(query, function (err, rows, fields) {
					if (!err) {
						result=rows;
					} else {
						console.log('Error', err);
					}
				});
			} else {
				console.log(err);
			}
		});
		console.log(result);   //All is OK here
		return result;
	};
The problem is the function which don't give the value back because it's asynchronous and so my "data" object is still empty when I try to use it. (I think)
Does anybody know a solution?
User avatar
coolbloke1324
Posts: 181
Joined: Mon Jan 23, 2012 5:20 pm

Re: MySQL in a separate function

Post by coolbloke1324 »

alex_king92 wrote:Hi, I need to make a function which query my MySQL db.
I've made this:
call:

Code: Select all

var data = {};
data = self.queryMySQL('SELECT * FROM table_one');
console.log(data);   //No output here
function:

Code: Select all

queryMySQL: function(query){
	var result = {};
	//connect to the mysql server
		ige.mysql.connect(function (err, db) {
			// Check if we connected to mysql correctly
			if (!err) {
				// Query the database
				ige.mysql.query(query, function (err, rows, fields) {
					if (!err) {
						result=rows;
					} else {
						console.log('Error', err);
					}
				});
			} else {
				console.log(err);
			}
		});
		console.log(result);   //All is OK here
		return result;
	};
The problem is the function which don't give the value back because it's asynchronous and so my "data" object is still empty when I try to use it. (I think)
Does anybody know a solution?
Hi, yes you are correct the data is async so not returned as a value from the function call.

The solution is to pass a callback method:

Code: Select all

queryMySQL: function(query, callback){
	var result = {};
	//connect to the mysql server
		ige.mysql.connect(function (err, db) {
			// Check if we connected to mysql correctly
			if (!err) {
				// Query the database
				ige.mysql.query(query, function (err, rows, fields) {
					if (!err) {
                                                callback(rows, fields);
					} else {
						console.log('Error', err);
					}
				});
			} else {
				console.log(err);
			}
		});
	};
Then call it via:

Code: Select all

var data = {};
data = self.queryMySQL('SELECT * FROM table_one', function (rows, fields) {
console.log(rows, fields);   //No output here
});
CEO & Lead Developer
Irrelon Software Limited
http://www.isogenicengine.com
alex_king92
Posts: 12
Joined: Fri Aug 30, 2013 9:21 am

Re: MySQL in a separate function

Post by alex_king92 »

Ok, but can I bring the result outside the function call? Something like this:

Code: Select all

var data = {};
self.queryMySQL('SELECT * FROM table_one', function (rows, fields) {
data=rows;
});
console.log(data);
alex_king92
Posts: 12
Joined: Fri Aug 30, 2013 9:21 am

Re: MySQL in a separate function

Post by alex_king92 »

I have solved, I used a global variable:

Code: Select all

self.data[0]=rows[0];
Now a have an other question. I dont understand how I have to call the "disconnect" metod of "IgeMySql"
When I call it, I have this answer from my cmd: "Cannot call methot 'end' of undefined...".
What I have to pass it to identify my current connection?
User avatar
coolbloke1324
Posts: 181
Joined: Mon Jan 23, 2012 5:20 pm

Re: MySQL in a separate function

Post by coolbloke1324 »

alex_king92 wrote:Ok, but can I bring the result outside the function call? Something like this:

Code: Select all

var data = {};
self.queryMySQL('SELECT * FROM table_one', function (rows, fields) {
data=rows;
});
console.log(data);
Hey, the best way to do that is to emit events rather than assign data to a global variable. That said, you should try to design your code so that you don't need to use the data outside of the callback.

This is standard javascript by the way, the engine isn't doing anything weird to make it callback-based just in case you wondered if it was :)
CEO & Lead Developer
Irrelon Software Limited
http://www.isogenicengine.com
User avatar
coolbloke1324
Posts: 181
Joined: Mon Jan 23, 2012 5:20 pm

Re: MySQL in a separate function

Post by coolbloke1324 »

alex_king92 wrote:I have solved, I used a global variable:

Code: Select all

self.data[0]=rows[0];
Now a have an other question. I dont understand how I have to call the "disconnect" metod of "IgeMySql"
When I call it, I have this answer from my cmd: "Cannot call methot 'end' of undefined...".
What I have to pass it to identify my current connection?
I think you've found a bug in the MySQL component. I've updated the code but I never use MySQL anymore so I cannot easily test.

In engine/components/database/mysql/IgeMySql.js line 73 where it says:
this.connection.end(function () {
can you change it to:
this.client.end(function () {
and let me know if it works? If so I will push the change.
CEO & Lead Developer
Irrelon Software Limited
http://www.isogenicengine.com
alex_king92
Posts: 12
Joined: Fri Aug 30, 2013 9:21 am

Re: MySQL in a separate function

Post by alex_king92 »

Done. And the output is this:
C:\wamp\www\ige-master\engine\components\database\mysql\IgeMySql.js:74
callback();
^
TypeError: undefined is not a function
at Quit._callback (C:\wamp\www\ige-master\engine\components\database\mysql\IgeMySql.js:74:4)
at C:\wamp\www\ige-master\server\node_modules\mysql\lib\protocol\sequences\Sequence.js:68:22
at process._tickCallback (node.js:415:13)

But if I modify it in this way:

Code: Select all

this.client.destroy();
It seems to work.
I don't know if this is an ige or node error, or an error is occurred while I installed my server. Any opinion?
User avatar
coolbloke1324
Posts: 181
Joined: Mon Jan 23, 2012 5:20 pm

Re: MySQL in a separate function

Post by coolbloke1324 »

alex_king92 wrote:Done. And the output is this:
C:\wamp\www\ige-master\engine\components\database\mysql\IgeMySql.js:74
callback();
^
TypeError: undefined is not a function
at Quit._callback (C:\wamp\www\ige-master\engine\components\database\mysql\IgeMySql.js:74:4)
at C:\wamp\www\ige-master\server\node_modules\mysql\lib\protocol\sequences\Sequence.js:68:22
at process._tickCallback (node.js:415:13)

But if I modify it in this way:

Code: Select all

this.client.destroy();
It seems to work.
I don't know if this is an ige or node error, or an error is occurred while I installed my server. Any opinion?
Hmm odd. Maybe I'm using an old version of the node module... I will update and check the API as it might have changed to destroy() now...
CEO & Lead Developer
Irrelon Software Limited
http://www.isogenicengine.com
alex_king92
Posts: 12
Joined: Fri Aug 30, 2013 9:21 am

Re: MySQL in a separate function

Post by alex_king92 »

Try again with end(), it should be the more graceful way to disconnect. Look this: https://github.com/felixge/node-mysql
Post Reply

Return to “HTML5/Web Engines”