admin管理员组文章数量:1405393
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!
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!
Share Improve this question asked Aug 3, 2012 at 13:34 freakimkaefigfreakimkaefig 4195 silver badges19 bronze badges1 Answer
Reset to default 5You can't return a value from an asynchronous callback like the one used by query
. You need to rewrite queryToDatabase so it takes a callback as its second argument and calls it with rows
when the query is successful. Then your main code needs to look like:
queryToDatabase(query, function(result) {
console.log(getTime() + "DB-Result Abfrage: " + result);
});
In reality, all your code that depends on the query result will need to go into that callback.
In practice, you'd want the callback to take two arguments: an error status and a result, with the error status being null
if the query was successful.
本文标签: javascriptnodemysql connectionquery() returns undefinedStack Overflow
版权声明:本文标题:javascript - node-mysql connection.query() returns undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744280606a2598631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论