NodeJS and MySQL: getting array data inside query callback - mysql

I'm trying to run this piece of code:
setInterval(function () {
var params = {
QueueUrl: 'https://sqs.us-east-1.amazonaws.com/821808622769/Teste', // required
MaxNumberOfMessages: 10
};
sqs.receiveMessage(params, function (err, data) {
if (err)
console.log('Erro de SQS:' + err);
else {
var retorno = data.Messages;
for (var i in data.Messages) {
var queryString = 'SELECT now()'; //dummy query, just for tests
db_connection.query(queryString, null, function (err, rows, fields) {
if (err) {
console.log('Erro no BD:' + err);
return;
}
var date = new Date();
console.log(retorno[i].Body + ' ' + date.getTime().toString());
});
}
console.log();
}
});
}, 30000);
and I have 5 messages in AWS SQS, such as this:
but when I run my code, instead of having the retorno[i].Body of each message, sometimes I get a message repeated, as show in this image
My for loop is running from 1 to 5 queries, but how do I carry the retorno[i] inside the callback to the database query? I mean, how do I identify which message I was dealing with?

Using .bind() you can setting the internal version of i to be the same as the version of i in your loop.
.bind({i:i}) at the end of your callback and change the internal code to reference this.i
for (var i in data.Messages) {
var queryString = 'SELECT now()'; //dummy query, just for tests
db_connection.query(queryString, null, function(err, rows, fields) {
if (err) {
console.log('Erro no BD:' + err);
return;
}
var date = new Date();
console.log(retorno[this.i].Body + ' ' + date.getTime().toString());
}.bind({
i: i
});
}
}

Related

Return boolean value from MYSQL in NodeJS

Im working on a function which will return a boolean-value. This value represents if an user exists in the database. Currently I have this:
function checkIfExists(){
var sql = "SELECT * FROM Users WHERE user = " + mysql.escape(req.body.username);
var rows = 0;
database.query(sql, function(err, result, fields){
console.log(result.length);
rows += result.length;
});
return rows > 0;
}
console.log(checkIfExists());
I use 'console.log(result.length)' to validate if there are results. When I test some input, I got this:
false
1
This is strange because there is one result, so rows should be equal to 1. But then it returns false instead of true. Is it possible that the value of rows isn't changed in 'database.query(...' ?
Because your function checkIfExists is asynchronous, I think you sould use callback system like this :
function checkIfExists(callback) {
var sql = "SELECT * FROM Users WHERE user = " + mysql.escape(req.body.username);
var rows = 0;
database.query(sql, function (err, result, fields) {
if (err) {
callback(err, null);
}
else {
console.log(result.length);
rows += result.length;
callback(null, rows > 0);
}
});
}
checkIfExists(function(err, isExists) {
if (err) {
// An error occured
}
else {
console.log(isExists);
}
});
EDIT
You also can simlify your checkIfExists function like this :
function checkIfExists(callback) {
var sql = "SELECT * FROM Users WHERE user = " + mysql.escape(req.body.username);
database.query(sql, function (err, result) {
callback(err, result ? result.length > 0 : false);
});
}
More information here :
Understanding Async Programming in Node.js
Hope it helps.

Node.js Checkbox Iteration

How do I iterate through the checked checkboxes, without hard coding and checking each box one at a time. Kinda like this link, but with Node.js and not JavaScript
Iterate all checkbox in JavaScript:
So I'm attempting to display the contents of my database (using MySQL), but I don't want to hard code 'req.body.checkboxName' for every checkbox in case a new column is added onto a table (the checkboxes are each of the columns in a given table). So I need to find a way that will iterate through every checkbox in the webpage and see if it is checked. If it is then we'll add them together in a string and query for those columns. My code below is for the Node.js POST method (after hitting submit once the checkboxes are checked) and the other is my Pug/Jade code (the JavaScript Templating Engine that I use, sorry it isn't plain html).
In case you're confused how the Pug/Jade file runs initially, another GET method renders that Pug/Jade file with the column names; that GET method is basically the POST method below up until the '//Need help' line
Node.js
app.post('/GetTables', function(req, res){
var columnsRequest = 'DESCRIBE ' + tableName;
var columnsList = [];
connection.query(columnsRequest, function(err, results, fields) {
if(err){
throw err;
}
for (var index in results) {
console.log(results[index].Field);
columnsList.push(results[index].Field);
}
});
//Need help, the next 5 or so lines are non-working ~psuedocode
var checkedList = '';
req.body.CHECKBOXES.each(function(index, element){
if(CHECKBOX.ischecked(){
checkedList += CHECKBOX.name() + ', '
}
});
var mysqlRequest = 'SELECT ' + checkedList + ' FROM ' + tableName;
connection.query(mysqlRequest, function(err, results, fields) {
if(err){
throw err;
}
res.render('webPage', {'columnstodisplay': results});
});
});
Pug/Jade
form(method = 'POST', action = '/GetTables', id = 'tableform')
fieldset
each item in columns
//this loop sets up the checkboxes for each of the columns
p
input(type="checkbox", name=item, value=item)
span #{item}
br
input(type ='submit', value ='Submit')
Thanks in advance!
Sorry for waiting a month to answer my own question. Below is the code that I have. Essentially what I did was when the submit button was clicked, it called a JavaScript function that sees which checkboxes are checked and creates two hidden elements; an array of checked checkboxes and the string to place in the MySQL query (the query asks for the columns/the checkboxes that were checked).
app.post('/GetTables', function(req, res){
var columnsRequest = 'DESCRIBE ' + tableName;
var columnsList = [];
connection.query(columnsRequest, function(err, results, fields) {
if(err){
throw err;
}
for (var index in results) {
console.log(results[index].Field);
columnsList.push(results[index].Field);
}
});
//changed the line below****
var mysqlRequest = 'SELECT ' + req.body.checkboxNames + ' FROM ' + tableName;
connection.query(mysqlRequest, function(err, results, fields) {
if(err){
throw err;
}
//changed the line below********
res.render('webPageName', {'columns': columnsList, 'rows': results, 'fields': fields, 'recheckboxes':req.body.checkboxArray});
});
});
Pug/Jade
form(method = 'POST', action = '/GetTables', id = 'tableform')
fieldset
each item in columns
//this loop sets up the checkboxes for each of the columns
p
input(type="checkbox", name=item, value=item)
span #{item}
br
input(type ='submit', value ='Submit', onclick="sendCBs(document.thisForm, '/gettables')")
script.
function sendCBs(form, path, method) {
var cbNames = '';
var cbArray = [];
var count = 0;
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].type == 'checkbox') {
if (form.elements[i].checked == true) {
cbNames += form.elements[i].value + ', ';
cbArray.push(form.elements[i].value);
count++;
}
}
}
if(count > 0){
cbNames = cbNames.replace(/,\s*$/, ""); //remove the last comma if 1 or more checkboxes selected
}
else{
return;
}
method = method || "POST"; //if not specified, method will be post
var inputform = document.createElement("form");
inputform.setAttribute("method", method);
inputform.setAttribute("action", path);
var hiddenField1 = createHiddenInput("checkboxNames", cbNames);
var hiddenField2 = createHiddenInput("checkboxArray", cbArray);
inputform.appendChild(hiddenField1);
inputform.appendChild(hiddenField2);
document.body.appendChild(inputform);
inputform.submit();
}

How to handle nodejs async with mysql?

Community,
I am new at nodejs and now i have a problem i cant solve: The async in javascript/nodejs. How can i handle the following so i can push the usernames to the array?
I already tried to help myself with many different functions but nothing works for me... :/
Sincerely Adhskid.
function getCurrentBetInformations () {
connection.query('SELECT * FROM `BETS` WHERE BET_ACTIVE = "1" LIMIT 1', function(err, rowss, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
betid = rowss[0].BET_ID;
betends = rowss[0].BET_END;
connection.query('SELECT * FROM `BETS_BID` WHERE BID_BET_ID=\'' + betid + '\'', function(err, betbids, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
var betQuants = new Array();
var betIds = new Array();
var betUsernames = new Array();
var betDates = new Array();
var rowsAffected = betbids.length;
for(i=0; i < rowsAffected; i++) {
betQuants.push(betbids[i].BID_KEYS_COUNT);
betIds.push(betbids[i].BID_ID);
var betSender = betbids[i].BID_SENDER;
connection.query('SELECT `USER_NAME` FROM `USER` WHERE `USER_STEAMID` = \'' + betSender + '\' LIMIT 1', function(err, rows, fields) {
if (err) logger.warn('MySQL Error: ' + err.stack);
console.log(rows[0].USER_NAME);
addUsername(rows[0].USER_NAME);
});
function addUsername (currentUsername) {
betUsernames.push(currentUsername);
}
betDates.push(betbids[i].BID_TIME);
if(betUsernames.length === i) {
execSiteRef();
}
}
function execSiteRef() {
console.log(betUsernames);
sendUserSiteRefresh([betQuants, betIds, betUsernames, betDates], betends);
}
});
});
}
I think your problem comes from this part:
if(betUsernames.length === i) {
execSiteRef();
}
You should iinstead check if the betUsernames array is of the final size:
if(betUsernames.length === rowsAffected) {
execSiteRef();
}
maybe there is more errors though, I did not check closely.

How to save the result of MySql query in variable using node-mysql [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 1 year ago.
im trying to save the result of MySql query in variable using node-mysql model and node.js so i have this code:
connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
console.log(rows);
}
});
and the result is :
[ { idRooms: 1, Room_Name: 'dd' },
{ idRooms: 2, Room_Name: 'sad' } ]
so i need to store this results in variable so i try like this:
var someVar = connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
return rows;
}
});
console.log(someVar);
but is not working thanks for any help in advance.
Well #Fadi, connection.query is async, which mean when your call your console.log(someVar), someVar has not been set yet.
What you could do:
var someVar = [];
connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
setValue(rows);
}
});
function setValue(value) {
someVar = value;
console.log(someVar);
}
You can't do that because network i/o is asynchronous and non-blocking in node.js. So any logic that comes afterwards that must execute only after the query has finished, you must place inside the query's callback. If you have many nested asynchronous operations you may look into using a module such as async to help better organize your asynchronous tasks.
As a complement to already given answers.
var **someVar** = connection.query( *sqlQuery*, *callback function( err , row , fields){}* )
console.log(**someVar**);
This construction will return in someVar information of this connection and his SQL query . It will not return values ​​from the query .
Values ​​from the query are located in the callback function ( err , row , fields)
Here is your answer if you want to assign a variable to res.render
//before define the values
var tum_render = [];
tum_render.title = "Turan";
tum_render.description = "Turan'ın websitesi";
//left the queries seperatly
var rastgeleQuery = "SELECT baslik,hit FROM icerik ORDER BY RAND() LIMIT 1";
var son5Query = "SELECT baslik,hit FROM icerik LIMIT 5";
var query_arr = [rastgeleQuery, son5Query];
var query_name = ['rastgele', 'son5'];
//and the functions are
//query for db
function runQuery(query_arr,sira){
connection.query(query_arr[sira], function(err, rows, fields) {
if (err) throw err;
var obj = [];
obj[query_name[sira]] = rows;
sonuclar.push(obj);
if (query_arr.length <= sira+1){
sonuc();
}else{
runQuery(query_arr, sira+1);
}
});
}
//the function joins some functions http://stackoverflow.com/questions/2454295/javascript-concatenate-properties-from-multiple-objects-associative-array
function collect() {
var ret = {};
var len = arguments.length;
for (var i=0; i<len; i++) {
for (p in arguments[i]) {
if (arguments[i].hasOwnProperty(p)) {
ret[p] = arguments[i][p];
}
}
}
return ret;
}
//runQuery callback
function sonuc(){
for(var x = 0; x<=sonuclar.length-1; x++){
tum_render = collect(tum_render,sonuclar[x]);
}
console.log(tum_render);
res.render('index', tum_render);
}

Access Multiple table concurrently Mysql Nodejs

I am trying to assing a structured array from two tables, First table select query from which the value in result fetch the ID and assing to the next query, Here is my code
var query = db.query('select * from orderdish'),
users = [];
query
.on('error', function(err)
{
console.log(err);
updateSockets(err);
})
.on('result', function(order,callback)
{
order.abc ='11';
order.OrderKOT=[];
var queryOrderKOT = db.query('select * from tblorderkot where order_Id='+ order.order_Id,function()
{
kotOrders = [];
queryOrderKOT
.on('error',function(err)
{
console.log(err);
updateSocket(err);
})
.on('result',function(orderKOT)
{
kotOrders.push(orderKOT);
})
.on('end', function()
{
console.log(kotOrders);
order.OrderKOT.push(kotOrders);
});
});
console.log(order);
users.push(order);
/* aa(function(){
});*/
})
.on('end', function()
{
// loop on itself only if there are sockets still connected
if (connectionsArray.length)
{
pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
console.log("This is End Values");
updateSockets({ users: users });
}
});
It's setting order.OrderKOT to empty. I know the it got to be done with call back in query.on(result) but if I set it's not fetching me any result. Second Query queryOrderKOT is working but it's fetching value pretty late and it's not pusing value to order.OrderKOT. Suggesst me for fetching the value concurrently.
It is likely that the "end" event for the first query is occurring before the second queryOrderKot query has had a chance to complete.
You should experience the expected behavior if you move the main response logic from the "end" of query to the "end" or "result" of queryOrderKot.
After so much of head breaking finally found the solution for concurrent mysql nodejs check #kevin Reilly thanks for trying a lot. I found out what ever we tried was streaming query. Which will be do the process async. Now coming back to the callback I wrote following and it's working perfectly
var query = db.query('select * from orderdish', function (err, results, fields, callback) {
if (err) {
console.log("ERROR: " + err.message);
updateSockets(err);
}
else {
// length of results
var count = 0;
// pass the db object also if you wanna use the same instance
q2(count, results, callback);
}
});
function q2(count, results, callbacks) {
var queryOrderKOT = db.query('select * from tblorderkot where order_Id=' + results[count].order_Id, function (err, resultKOT, KOTFields, callback) {
console.log(resultKOT);
results[count].OrderKOT = resultKOT;
count++;
if (count < results.length) {
q2(count, results, callback);
}
else {
// do something that you need to do after this
console.log(results);
//callbacks(results);
}
});
}
Here is my whole Code,
var mysql = require('mysql')
var io = require('socket.io').listen(3000)
var db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'vsk',
database: 'hahaha'
})
var POLLING_INTERVAL = 3000, connectionsArray = [], pollingTimer;
db.connect(function (err) {
if (err) console.log(err)
})
var socketCount = 0, restID = 0;
var pollingLoop = function () {
var query = db.query('select * from orderdish'), users = [];
.on('error', function (err) {
console.log(err);
updateSockets(err);
}).on('result', function (order) {
console.log("THis is USerID" + order.order_Id);
order.abc = '11';
order.OrderKOT = [];
var queryOrderKOT = db.query('select * from tblorderkot where order_Id=' + order.order_Id);
kotOrders = [];
queryOrderKOT.on('error', function (err) {
console.log(err);
updateSocket(err);
}).on('result', function (orderKOT) {
kotOrders.push(orderKOT);
query.on('end', function () {
console.log("This is at end values");
// loop on itself only if there are sockets still connected
if (connectionsArray.length) {
pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
console.log("This is End Values");
updateSockets({
users: users
});
}
});
}).on('end', function () {
console.log(kotOrders);
console.log("This is my KOT End");
order.OrderKOT.push(kotOrders);
users.push(order);
console.log(users);
//callback(kotOrders);
});
console.log(order);
});
};
io.sockets.on('connection', function (socket) {
console.log("New Connection Opened");
socket.on('restID', function (restIDs) {
console.log(restIDs);
restID = restIDs;
console.log('Number of connections:' + connectionsArray.length);
if (connectionsArray.length > 0) {
console.log("Here is the Pooling Loop");
pollingLoop();
}
});
socket.on('disconnect', function () {
var socketIndex = connectionsArray.indexOf(socket);
console.log('socket = ' + socketIndex + ' disconnected');
if (socketIndex >= 0) {
connectionsArray.splice(socketIndex, 1);
}
});
connectionsArray.push(socket);
});
var updateSockets = function (data) { // adding the time of the last update data.time = new Date();
// sending new data to all the sockets connected connectionsArray.forEach(function(tmpSocket) {
// tmpSocket.volatile.emit('notification', data); console.log(data); tmpSocket.volatile.emit('notification', data); }); };