Nested mysql query in each loop - mysql

So all i want to do is to make a query to get all id's from the database and then i want to do my method for the id and update the row in the database.
So i created this code. But all it is doing is to get the ids and do not the nested query with the insertString1.
var mysql = require('mysql');
var _ = require("underscore");
var connection = mysql.createConnection({
host : 'myHost.net',
user : 'myUser',
password : 'myPassword',
database : 'myDatabase'
});
connection.query('SELECT * FROM `idmarkethash`', function (error, results) {
_.each(results, function (row) {
// here i want to calculate the new values of the row
// This insert query is just an example
var insertString1 = "UPDATE idmarkethash SET " +
"Price=" + mysql.escape(2.00) + ", " +
"Gradient=" + mysql.escape(2.00) + ", " +
"LastUpdated=" + mysql.escape("2015-08-31 00:00:11") + " " +
"WHERE Id=" + mysql.escape(181) + ";";
connection.query(insertString1, function (error1, result1) {
console.log(result1);
});
});
});
I tried the same with the async package:
var async = require('async');
// Packages are all included.
connection.query('SELECT * FROM `idmarkethash`', function (error, results) {
async.each(results, function (row, callback) {
// here i want to calculate the new values of the row
// This insert query is just an example
var insertString1 = "UPDATE idmarkethash SET " +
"Price=" + mysql.escape(2.00) + ", " +
"Gradient=" + mysql.escape(2.00) + ", " +
"LastUpdated=" + mysql.escape("2015-08-31 00:00:11") + " " +
"WHERE Id=" + mysql.escape(181) + ";";
connection.query(insertString1, function (error1, result1) {
console.log(result1);
callback();
});
});
});
With debugging i could see that the declaration of insertString1 is called, but the second query is not fired / or the loop continues without recognizing the second query.

Related

Nodejs with mysql query not giving dynamic value from database

I am trying to upload a file in two location, it is working find when I am testing it from postman. But when we trying to upload file from my mobile app without it return an error.
one interesting thing: when after restarting server if we first hit from postman then it is working from mobile app as well but in that case dynamic value to not updating, Value came from postman hit is used in dynamic field.
// app.get('/empid/:id', (req, res) => {
app.post('/upload/:id', function(req,res){
pool.query('SELECT first_name, username FROM user_detail where user_detail_pk=?', [req.params.id], (err, rows, fields) => {
if (!err)
res.send(rows);
else
console.log(err);
rows.forEach( (row) => {
console.log(`${row.first_name}`);
var fname = (`${row.first_name}`);
var username = (`${row.username}`);
console.log('First Name is : ' + fname);
console.log('UserName Name is : ' + username);
var dateObj = new Date();
//var month = dateObj.getUTCMonth() + 1; //months from 1-12
var year = dateObj.getUTCFullYear();
var month = dateObj.toLocaleString('default', { month: 'long' });
var day = dateObj.getUTCDate();
datewise = "datewise" + "/" + year + "/" + month + "/" + day + "/" + fname + "(" + username + ")";
empwise = "empwise" + "/" + fname + "(" + username + ")" + "/" + year + "/" + month + "/" + day;
});
var storage = multer.diskStorage({
destination: function (req, file, cb) {
let Id = req.body.id;
let datewisepath = `/home/KunDan/RestAPIUpload/Conveyance/${datewise}`;
let empwisepath = `/home/KunDan/RestAPIUpload/Conveyance/${empwise}`;
fs.mkdirsSync(datewisepath);
fs.mkdirsSync(empwisepath);
cb(null, datewisepath);
cb(null, empwisepath);
// callback(null, 'uploads/' + req.user.id);
},
/* filename: function (req, file, callback) {
callback(null, file.originalname + '-' + Date.now());
}
});
*/
filename: function (req, file, cb) {
console.log(file);
let extArray = file.mimetype.split("/");
let extension = extArray[extArray.length - 1];
cb(null, file.originalname + '-' + Date.now() + "." + extension);
console.log(req.file);
}
})
var upload = multer({ storage : storage }).array('dataFile',50);
upload(req,res,function(err) {
console.log(req.body);
console.log(req.files);
if(err) {
return res.end("Error uploading file.");
}
//return res.end("File is uploaded");
else
("File is uploaded");
});
});
});
Error Screenshot is Here

NodeJS and MYSQL Issue

My code is supposed to retrieve the "DropsRemaining" for a user ID (which it does successfully) and then -1 from the number it retrieves. When retreiving the data it returns this string "[ RowDataPacket { DropsRemaining: 5 } }" however the end of the code does not -1 from the DropsRemaining it instead sets the DropsRemaining to -1. If anyone can help fix this I'd really appreciate it.
var sql = "SELECT DropsRemaining FROM UserData WHERE DiscordID LIKE " + message.author.id;
var DropCount = [];
connection.query(sql, function (err, result) {
if (!err)
setValue(result);
else
console.log("No Information For That User Found");
});
function setValue(value) {
DropCount = value;
console.log(DropCount);
};
//Remove drop from user
DropCount = DropCount - 1;
var sql = "UPDATE UserData SET DropsRemaining = " + DropCount + " WHERE DiscordID = " + message.author.id;
The problem is that the order you write your Javascript code is not exactly how it is performed in the end.
When you call the connection.query() function, the next line of code does not necessarily already have the result of that function.
I would recommend you to have a look on this book series, they have really good explanations about those characteristics.
Probably the following code will output the expected response. Notice that I nested the code, therefore I can control the flow properly.
var sql = "SELECT DropsRemaining FROM UserData WHERE DiscordID LIKE " + message.author.id;
// Get the DropsRemaining
connection.query(sql, function (err, result) {
if (!err) {
// No errors in the query, decrement the Drops
decrementDrop(result);
} else {
console.log("No Information For That User Found");
}
});
function decrementDrop(dropsAvailable) {
var dropsRemaining = dropsAvailable - 1;
var updateSql = "UPDATE UserData SET DropsRemaining = " + dropsRemaining + " WHERE DiscordID = " + message.author.id;
// Update the DropsRemaining column to the dropsRemaining, i.e., decrement the DropsRemaining column value
connection.query(updateSql, function (err, result) {
if (!err) {
console.log("DiscordID = " + message.author.id +" has " + dropsRemaining + " drops remaining")
} else {
console.log("Error!");
}
});
}

Can't parse JSON returned from MySQL

I have a Node.js app that is writing data to a MySQL backend. One field is an array I stringify. I can see in the workbench the data is correct when in the database. However when I retrieve it I am getting an error when I try to parse it.
"Unexpected token o in JSON at position 1"
If I log the result it shows up as [Object Object].
From what I read online this means it is already a JS object and I do not need to parse it. However I cannot find anything about how to get access to the data.
process: function (bot, msg, suffix) {
var ftcmds = suffix.split(" ", 1);
var ftName = ftcmds[0];
var ftArray;
var selectSQL = "SELECT FireTeam FROM fireteam WHERE Name = '" + ftName + "'";
var updateSQL = "UPDATE fireteam SET FireTeam = '" + ftArray + "'WHERE Name = '" + ftName + "'";
mysqlcon.query(selectSQL, function (err, result) {
console.log("Result |" + result);
console.log("Error |" + err);
if (err) {
console.log("Caught Error " + err + " " + msg.author);
}
else {
console.log("Recovered result " + result);
ftArray = result;
console.log("Attempting to update array");
ftArray.push(msg.author.id);
console.log("updated array " + ftArray);
var jsonArray = JSON.stringify(ftArray);
mysqlcon.query(updateSQL, function (err, result) {
console.log("Result |" + result);
console.log("Error |" + err);
if (err.toString().indexOf(dupErr) != -1) {
msg.author.send("Could not find that fireteam");
console.log("Error: Did not locate the requested name " + msg.author)
} else if (err) {
console.log("Caught Error " + err + " " + msg.author);
}
else {
msg.author.send("You have joined Fireteam " + name + ". I will setup a group chat on " + date + " if your team fills up.");
}
})
}
});
}
You should just be able to access it as an object, so if result has fields name and title you can just access them as:
var name = result.name
var title = result.title

Loop through MySQL rows and store results in array

I am trying to store details of affectedRows from a MySQL INSERT query using NodeJS. My mind is melting trying to comprehend callbacks and Promises. Being a single-man dev team I wanted to reach out and ask for the clearest explanation as to how a callback can be applied here in a foreach loop.
The goal should be clear from these few lines of code; store data in the affected_rows[] array.
var affected_rows = [];
asset_array.forEach(function(asset) { // Populate the asset table
var query_string = "INSERT IGNORE INTO " + asset_table + " SET symbol = '" + asset[0] + "', name = '" + asset[1] + "'";
connection.query(query_string, function(err, rows, fields) {
if (err) throw err;
if ( rows.affectedRows > 0 ) {
data_to_push = [asset_table, asset[0], asset[1]];
affected_rows.push(data_to_push);
}
});
});
console.log(affected_rows); // [] for obvious async reasons
One option would be to process the asset_array inside a function and pass a callback into it and when loops through asset_array check if the current index matches the asset_array length (-1). If so call the callback.
var affected_rows = [];
function processAssets(cb) {
var array_len = asset_array_len.length
asset_array.forEach(function(asset, index) {
var query_string = 'INSERT IGNORE INTO ' + asset_table + ' SET symbol = \'' + asset[0] + '\', name = \'' + asset[1] + '\'';
connection.query(query_string, function(err, rows, fields) {
if (err) throw err
if (rows.affectedRows > 0) {
data_to_push = [asset_table, asset[0], asset[1]];
affected_rows.push(data_to_push);
}
if (index === (array_len - 1)) cb()
});
});
}
processAssets(function() {
console.log(affected_rows)
})
Will suggest you to have a look at async Queue.
You can change your code like this to use it.
//2nd Step - Perform each task and then call callback() to move to next task
var q = async.queue(function(query_string, callback) {
connection.query(query_string, function(err, rows, fields) {
if (err) throw err;
if ( rows.affectedRows > 0 ) {
data_to_push = [asset_table, asset[0], asset[1]];
affected_rows.push(data_to_push);
}
callback(); //call next task
});
}, 2); //here 2 means concurrency ie 2 tasks will run in parallel
//Final Step - Drain gives you end of queue which means all tasks have finished processing
q.drain = function() {
//Do whatever you want after all tasks are finished
};
//1st Step - create a queue of all tasks that you need to perform
for (var i = 0; i < asset_array.length ; i++) {
var query_string = "INSERT IGNORE INTO " + asset_table + " SET symbol = '" + asset[0] + "', name = '" + asset[1] + "'";
q.push(query_string);
}

Nodejs multiple sql query loop

I am pretty new to nodejs and async worlds.
The case is, I have an array like var ids = [1, 2, 3, 4];
I need to update mytable according to sequence of the array element. So I do something like:
sort: function(ids, callback) {
// dont worry about this
this.create_connection();
this.connection.connect();
for(var i=0; i<ids.length;i++) {
var q = "UPDATE mytable SET sequence="+i+" where id="+ids[i]+"; ";
this.connection.query(q, function(err, result) {
// I am not sure about this
// callback(err);
});
}
// I need to return callback at the end
// return callback();
this.connection.end();
}
But yes.. it does not work because I have to return callback.. I think I need to do the query syncronously.. I am not sure. Please help thanks.
If you are new to async worlds, you should take a look at module 'async'.
You can then do something like this :
async.forEachOfSeries(ids, function(id,index,callback){
var q = "UPDATE mytable SET sequence="+index+" where id="+id+"; ";
this.connection.query(q, function(err, result) {
callback();
});
},function done(){
// whatever you want to do onces all the individual updates have been executed.
})
See my inline comments:
sort: function(ids, callback) {
this.create_connection();
this.connection.connect();
var q = "UPDATE mytable SET sequence CASE id ";
// Don't execute one query per index in ids - that's inefficient
// Instead, pack up all the queries and execute them at once
for(var i=0; i<ids.length;i++) {
q += "WHEN " + ids[i] + " THEN " + i + " ";
}
q += "ELSE sequence END;";
// The sort method will return the result of connection.query
return this.connection.query(q, function(err, result) {
// End the connection
this.connection.end();
if(err) {
// Handle any error here
return callback(err);
}
// Otherwise, process, then return the result
return callback(err, result);
});
}
And here's something slightly more elegant:
sort: function(ids, callback) {
this.create_connection();
this.connection.connect();
// Don't execute one query per index in ids - that's inefficient
// Instead, pack up all the queries and execute them at once
var q = ids.reduce(function(pv, cv, ci){
return pv + " WHEN " + cv + " THEN " + ci + " ";
}, "UPDATE mytable SET sequence CASE id ") + " ELSE sequence END;";
// The sort method will return the result of connection.query
return this.connection.query(q, function(err, result) {
// End the connection
this.connection.end();
if(err) {
// Handle any error here
return callback(err);
}
// Otherwise, process, then return the result
return callback(err, result);
});
}
And you can replace the .reduce in the previous example with the following, if you want to use ES6 arrow functions:
var q = ids.reduce((pv, cv, ci) => pv + " WHEN " + cv + " THEN " + ci + " ",
"UPDATE mytable SET sequence CASE id ") + " ELSE sequence END;";