Page 1 of 1
MySQL in a separate function
Posted: Tue Sep 24, 2013 3:35 pm
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?
Re: MySQL in a separate function
Posted: Wed Sep 25, 2013 8:43 pm
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
});
Re: MySQL in a separate function
Posted: Thu Sep 26, 2013 5:18 pm
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);
Re: MySQL in a separate function
Posted: Thu Sep 26, 2013 8:48 pm
by alex_king92
I have solved, I used a global variable:
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?
Re: MySQL in a separate function
Posted: Fri Sep 27, 2013 8:13 pm
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

Re: MySQL in a separate function
Posted: Fri Sep 27, 2013 8:18 pm
by coolbloke1324
alex_king92 wrote:I have solved, I used a global variable:
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.
Re: MySQL in a separate function
Posted: Sun Oct 06, 2013 11:22 am
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:
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?
Re: MySQL in a separate function
Posted: Sun Oct 06, 2013 11:31 am
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:
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...
Re: MySQL in a separate function
Posted: Mon Oct 07, 2013 7:52 am
by alex_king92
Try again with end(), it should be the more graceful way to disconnect. Look this:
https://github.com/felixge/node-mysql