I feel a bit dumbstruck right now. I am fairly new to nodejs and javaScript and can't figure this one out. I guess it is because of the async nature of queries to mysql...
I made an example that shows my problem. I just want to cycle over a number of sql queries and do stuff with the results. for the sake of the example I just print out stuff. I know that I could use a single sql query like this "SELECT id, name FROM player WHERE id IN (1,2,3,4,5)" but this is not possible in the real application I am trying to write.
this is the relevant part of my nodejs app.js
var mysql = require("mysql");
var mysqlPool = mysql.createPool(conf.mysqlArbData);
for (var i = 0; i<5; i++){
mysqlPool.getConnection(function(err, connection) {
var detailSql = "SELECT id, name FROM player " +
"WHERE id = "+i;
if (err){
throw err;
}
connection.query(detailSql, function(err, detailRows, fields) {
connection.end();
console.log("detailSql="+detailSql);
if (err){
console.log("can't run query=" + detailSql +"\n Error="+err);
}
else{
console.log(detailRows[0].id + " " +detailRows[0].name);
}
});
});
};
Now the output:
web server listening on port 3000 in development mode
detailSql=SELECT id, name FROM player WHERE id = 5
5 Jyvaskyla
detailSql=SELECT id, name FROM player WHERE id = 5
5 Jyvaskyla
detailSql=SELECT id, name FROM player WHERE id = 5
5 Jyvaskyla
detailSql=SELECT id, name FROM player WHERE id = 5
5 Jyvaskyla
detailSql=SELECT id, name FROM player WHERE id = 5
5 Jyvaskyla
My question is, why do I get only the result for database entry with id=5? What needs to be changed in order to receive each individual result in the callback?