I'm using a node ws server built on einaros/ws. That server has to send queries to a database. For that i'm using felixge/node-mysql.
When a user connects to the server, it should be checked if the client still exists in the database.
Here is the interesting piece of code:
console.log("client-liste ist leer");
var query = "SELECT name FROM spieler WHERE name='"+id+"' AND passwort='"+passwort+"'";
var result = queryToDatabase(query);
console.log(getTime() + "DB-Result Abfrage: " + result);
And the Code where the query is done:
var mysql = require('mysql');
var mysqlConnection = mysql.createConnection({
host: dbHost,
user: dbUser,
password: dbPassword,
});
function queryToDatabase(anfrage) {
mysqlConnection.connect();
mysqlConnection.query("USE " + db, function(err, rows, fields) {
if (err) throw err;
});
mysqlConnection.query(anfrage, function(err, rows, fields) {
if (err) throw err;
console.log("rows as String - " + JSON.stringify(rows));
return rows;
});
mysqlConnection.end();
}
The Logs in the console are:
client-liste ist leer
3.8.2012 - 15:29:0 - DB-Result Abfrage: undefined
rows as String - [{"name":"lukas"}]
Does anyone understand, why the function returns undefined? I also tried to wait for Database-Connection to finish with a simple setTimout, but nothing changed!