How to handle nodejs async with mysql? - 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.

Related

nodejs mysql pool connection just idle

I am creating a nodejs module which retrieve some data from a mysql database and insert into another mysql database after some data processing. My main requirement is to make the module alive 24 hours even there is no data in the first database.. it will just keep checking for any new data. But unfortunately the module just doing nothing after few minutes of running. My function is as follows:
var to_pool = mysql.createPool({
connectionLimit: 100,
host: 'localhost',
user: 'username',
password: 'password',
database: 'toDatabase',
multipleStatements: true
});
var from_pool = mysql.createPool({
connectionLimit: 100,
host: 'localhost',
user: 'username',
password: 'password',
database: 'fromDatabase'
});
get_data(to_pool, from_pool);
var items_per_query = 100;
function get_data(to_pool, from_pool) {
from_pool.getConnection(function (err, from_connection) {
if (err) throw err; // not connected!
//main database query
from_connection.query("SELECT p.*, d.uniqueid as imei FROM tc_positions p left join tc_devices d on d.id = p.deviceid order by p.id desc limit " + items_per_query, function (err, result, fields) {
if (err) throw err;
var items = [];
if (Object.keys(result).length > 0) {
Object.keys(result).forEach(function (key) {
var x = result[key];
items.push({ 'id': x['id'], 'table_name': x['imei'], 'table_columns': table_columns_list });
});
}
if (items.length >= items_per_query) {
var items_to_be_removed = [];
let imei_insert = "";
let insert_data = "";
for (var x = 0; x < items.length; x++) {
let all_values = "";
let i = 0;
for (let v of Object.values(items[x]['table_columns'])) {
i++;
all_values += "'" + v + "'";
if (i < Object.keys(items[x]['table_columns']).length) {
all_values += ",";
}
}
insert_data += "INSERT INTO " + items[x]['table_name'] + "(dt_server,dt_tracker,lat,lng,altitude,angle,speed,params,fix_time,accuracy,network) VALUES(" + all_values + "); ";
items_to_be_removed.push(items[x]['id']);
if (items_to_be_removed.length == items_per_query) {
var final_query = imei_insert + ' ' + createTable + ' ' + insert_data;
to_pool.getConnection(function (err, platform_connection) {
if (err) throw err;
platform_connection.query(final_query, function (err, results, fields) {
if (err) throw err;
var ids = items_to_be_removed.join(",");
from_connection.query("DELETE FROM tc_positions where id IN(" + ids + ")", function (err, results, fields) {
if (err) throw err;
console.log('removed ' + items_to_be_removed.length + ' rows from traccar');
items_to_be_removed = [];
insert_data = "";
from_connection.destroy();
platform_connection.destroy();
// after finish all task call the same function again
return get_data(to_pool, from_pool);
});
});
});
}
}
}
else {
setInterval(function () { get_data(to_pool, from_pool); }, 10000);
}
});
});
}
the get_data() function is being called every 10 secs but the "main database query" portion never execute after sometimes. Is there any way to execute the database query again and again as the get_data() function call?
it is better to use a package manager like PM2 and start your script like this
pm2 start app.js
no need to setup intervals in your code, let the code run and exit, PM2 will restart it automatically when it stops running, this will make your code alive 24 hours as per your requirement, you can also setup delays or setup restart strategies

How to insert json into mysql using node js?

var express = require('express');
var app=express();
var length;
var affiliate = require('flipkart-affiliate');
var url = require('url');
var moment=require('moment');
var mysql = require('mysql');
var body;
var getUrl;
var product;
var offer;
var offer1;
var offer2;
var offer3;
var test1;
var test2;
var test3;
var title=[];
var description=[];
var startTime=[];
var endTime=[];
var json={};
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'coupontest'
});
var client = affiliate.createClient({
FkAffId: 'anandhkum',
FkAffToken: 'eb030998c556443087d3b1a27ac569d0',
responseType: 'json'
});
client.getCategoryFeed({
trackingId: 'anandhkum'
}, function(err, result,getUrl){
if(!err){
body=JSON.parse(result);
getUrl=body.apiGroups.affiliate.apiListings.food_nutrition.availableVariants["v1.1.0"].get;
client.getProductsFeed({
url: getUrl
}, function(err, result){
if(!err){
}else {
console.log(err);
}
});
}
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
app.get('/',function (req,res) {
client.getAllOffers(null,function(err, resp) {
if (!err) {
offer = JSON.parse(resp);
test1 = offer.allOffersList.length;
res.send(offer);
for(var i=0;i<test1;i++){
description[i]=offer.allOffersList[i].description;
startTime[i]=offer.allOffersList[i].startTime;
endTime[i]=offer.allOffersList[i].endTime;
}
var stmt = "INSERT INTO offers(description,start_time,end_time) VALUES (?, ?, ?)";
connection.query(stmt, [description,startTime,endTime], function (err, result) {
if (err) throw err.message;
console.log("Number of records inserted: " + result.affectedRows);
});
}
else {
console.log(err);
}
});
});
app.listen(3000);
console.log("Listening to port 3000");
the code is about getting the offer details as json data from flipkart.com and store it into the mysql table.
But I'm getting the error
throw err; // Rethrow non-MySQL errors
ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1
I tried using many sql syntax but it's showing the above error
Can anyone help me on solving the above error?
Try doing it this way, as I can see you want to insert multiple result at once so need to pass an array values which I have made in following query also the error "ER_WRONG_VALUE_COUNT_ON_ROW" is due to incorrect no of ? that you were passing in the query as you need to insert multiple no of rows so you need to make sure you get correct ? count.
var queryString = "";
var insertString = "(?,?,?),";
var values = [];
for(var i=0;i<test1;i++){
description[i]=offer.allOffersList[i].description;
startTime[i]=offer.allOffersList[i].startTime;
endTime[i]=offer.allOffersList[i].endTime;
values.push(description[i],startTime[i],endTime[i]);
queryString = queryString + insertString;
}
queryString = queryString.substring(0, queryString.length - 1); // is used to remove last ',' which will get inserted while we are creating queryString
var stmt = "INSERT INTO offers(description,start_time,end_time) VALUES " + queryString ;
connection.query(stmt,values, function (err, result) {
if (err) throw err.message;
console.log("Number of records inserted: " + result.affectedRows);
});

NodeJS and MySQL: getting array data inside query callback

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
});
}
}

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); }); };

Synchronous mysql in Node.js

I know that node.js is event driven and I should do this async but i can't find in my mind a way to do it.
so, i have this
var querystr = "SELECT * FROM groups";
var result = "";
dbClient.query(querystr, function (err, res, fields) {
if (err) {
console.log(err);
return;
}
for(var i in res) {
result = result + "#" +res[i].name + " =";
for (var j in res[i].members.split(",")) {
var memberquery;
if (j.substr(0,1) == "#") {
memberquery = "SELECT name FROM groups WHERE id = "+j.substr(1, j.length-1);
} else {
memberquery = "SELECT username FROM users WHERE id= "+j;
}
dbClient.query(memberquery, function(err, memres, fields) {
var membername = "";
if (typeof memres[0].username == "undefined") {
membername = "#"+memres[0].name;
} else {
membername = memres[0].username;
}
result = result + " " + membername;
});
}
result = result + "\n";
}
});
The issue that makes it sync is the for inside.
basically i'm generating a document in the result variable where i check the groups and tell the members so the expected output is
Group1 = member, member
Group2 = member, member
I usually use a pattern like the one below for this type of problem. In a nutshell: get a list of things then call a function to handle the list; that function calls itself until the list is completed; gather up the results as you go in an accumulator; when the list is empty, return whatever you've accumulated through the callback. It's just another way of accomplishing what #Andrey Sidorov demonstrated in his response.
//cb is (err, res)
function getData(cb){
var querystr = "SELECT * FROM groups";
var result = "";
dbClient.query(querystr, function (err, res, fields) {
if (err)
cb(err);
else {
var groups = [];
for (var ndx in res)
groups = groups.concat(res[ndx].members.split(","));
getMembers(groups, [], cb);
}
});
}
function getMembers(members, sofar, cb){
var member = members.shift();
if (!member)
cb(null, sofar);
else {
var memberquery;
var params;
if (member.substr(0,1) == "#") {
memberquery = "SELECT name FROM groups WHERE id = ?";
params = [member.substr(1, member.length-1)];
} else {
memberquery = "SELECT username FROM users WHERE id = ?";
params = [member];
}
dbClient.query(memberquery, params, function(err, res) {
if (err)
cb(err);
else {
var membername = "";
if (typeof res[0].username == "undefined") {
membername = "#" + res[0].name;
} else {
membername = res[0].username;
}
sofar.push(membername);
getMembers(members, sofar, cb);
}
});
}
}
function do_queries( resultCallback )
{
var querystr = "SELECT * FROM groups";
var result = "";
var num_queries_left = 0;
dbClient.query(querystr, function (err, res, fields) {
if (err) {
console.log(err);
return;
}
// calculate number of invocations of second sub-query
for(var i in res)
num_queries_left += res[i].members.split(",").length;
for(var i in res) {
result = result + "#" +res[i].name + " =";
for (var j in res[i].members.split(",")) {
var memberquery;
if (j.substr(0,1) == "#") {
memberquery = "SELECT name FROM groups WHERE id = "+j.substr(1, j.length-1);
} else {
memberquery = "SELECT username FROM users WHERE id= "+j;
}
dbClient.query(memberquery, function(err, memres, fields) {
var membername = "";
if (typeof memres[0].username == "undefined") {
membername = "#"+memres[0].name;
} else {
membername = memres[0].username;
}
result = result + " " + membername;
num_queries_left--;
if (num_queries_left == 0)
{
resultCallback(result);
return;
}
});
}
result = result + "\n";
}
});
}
do_queries( function(result) {
console.log(result);
});