Node JS for loop and database query - mysql

I'm running a for loop on a function, and the database is updated on each loop. It then calls the database again using the for loop to update values on the database. However it seems to appear like i'm getting cached results. Is there any reason for this error?
I can post my code, but it's kind of tedious.
It's a simple for loop on a module function that calls a database request, then updates that database.
module.exports.newElectronicHyperCredit = function(request){
gt = request.body.custom + "-" + Date.now()
db.query(
"SELECT * FROM Tenderizer.Stats ORDER BY ID DESC LIMIT 1",
[
],
function(error, stats){
bsr = stats[0]['BuySellRate'];
dv = stats[0]['DollarValue'];
credits = stats[0]['Credits'];
for(i = 0; i < request.body.quantity; i++){
bsr = dv / credits;
dv = Number(dv) + 10;
credits = Number(credits) + (7 / BSR);
denomination = 7 / bsr;
db.query(
"INSERT INTO Tenderizer.Stats SET ?",
{
BuySellRate: bsr,
DollarValue: dv,
Credits: credits
},
function(error, stats){
}
)
db.query(
"SELECT * FROM Tenderizer.Withdrawals ORDER BY case when Withdrawals.Owner = 'Rah1337' then 1 else 2 end, Withdrawals.ID DESC",
[
],
function(error, withdrawals){
withdrawn = 0;
for(x in withdrawals){
if(denomination != 0){
if(withdrawals[x]['Denomination'] > denomination){
withdrawn = withdrawn + denomination;
db.query(
"UPDATE Tenderizer.Withdrawals SET Denomination = Denomination - ? WHERE ID = ?",
[
denomination,
withdrawals[x]['ID']
],
function(error, points){
}
);
db.query(
"INSERT INTO Tenderizer.Points SET ?",
{
GenerationTag: withdrawals[x]['GenerationTag'],
Owner: request.body.custom,
Denomination: denomination,
BoughtPoint: bsr,
Since: Date.now()
},
function(error, points){
}
);
db.query(
"INSERT INTO Tenderizer.Payouts SET ?",
{
GenerationTag: withdrawals[x]['GenerationTag'],
Owner: withdrawals[x]['Owner'],
DollarValue: bsr * withdrawals[x]['Denomination'],
Denomination: denomination,
Processed: 0,
Since: Date.now()
},
function(error, payouts){
}
);
denomination = 0;
}else{
withdrawn = withdrawn + denomination;
db.query(
"DELETE FROM Tenderizer.Withdrawals WHERE ID = ?",
[
withdrawals[x]['ID']
],
function(error, withdrawals){
}
);
db.query(
"INSERT INTO Tenderizer.Points SET ?",
{
GenerationTag: withdrawals[x]['GenerationTag'],
Owner: request.body.custom,
Denomination: withdrawals[x]['Denomination'],
BoughtPoint: bsr,
Since: Date.now()
},
function(error, points){
}
);
db.query(
"INSERT INTO Tenderizer.Payouts SET ?",
{
GenerationTag: withdrawals[x]['GenerationTag'],
Owner: withdrawals[x]['Owner'],
DollarValue: bsr * withdrawals[x]['Denomination'],
Denomination: withdrawals[x]['Denomination'],
Processed: 0,
Since: Date.now()
},
function(error, payouts){
}
);
denomination = denomination - withdrawals[x]['Denomination'];
}
}
}
if(denomination > 0){
db.query(
"INSERT INTO Tenderizer.Pool SET ?",
{
GenerationTag: gt,
Username: request.body.custom,
DollarValue: 10 - (withdrawn * bsr),
Withdrawn: 0,
Since: Date.now()
},
function(error, pool){
}
);
db.query(
"INSERT INTO Tenderizer.Points SET ?",
{
GenerationTag: gt,
Owner: request.body.custom,
Denomination: denomination,
BoughtPoint: bsr,
Since: Date.now()
},
function(error, points){
}
);
}
db.query(
"UPDATE Tenderizer.EHC_Count SET Credits = Credits + ? WHERE Username = ?",
[
7 / bsr
],
function(error, ehc_count){
}
);
}
);
}
}
);
};
It's not inserting the records one by one, it's inserting it all at once or it's using cached results. Because the rows that i'm recieving in my database are
duplicate rows rather than incrementing rows. In the points table in the denomination column and the bought point.

You're using lots of callbacks, but you're not actually doing anything to wait until those callbacks are called. This is difficult to resolve using callbacks, but could be done much more easily if you use a library that supports Promises.
For example, this is what the code would look like if you properly await each query using #databases/mysql
const createConnectionPool = require('#databases/mysql');
const {sql} = require('#databases/mysql');
const db = createConnectionPool();
module.exports.newElectronicHyperCredit = async function(request){
let gt = request.body.custom + "-" + Date.now()
const stats = await db.query(
sql`SELECT * FROM Tenderizer.Stats ORDER BY ID DESC LIMIT 1`,
);
let bsr = stats[0]['BuySellRate'];
let dv = stats[0]['DollarValue'];
let credits = stats[0]['Credits'];
for(let i = 0; i < request.body.quantity; i++){
bsr = dv / credits;
dv = Number(dv) + 10;
credits = Number(credits) + (7 / BSR);
denomination = 7 / bsr;
await db.query(sql`
INSERT INTO Tenderizer.Stats (BuySellRate, DollarValue, Credits)
VALUES (${bsr}, ${dv}, ${credits}
`)
const withdrawals = await db.query(sql`
SELECT * FROM Tenderizer.Withdrawals
ORDER BY case when Withdrawals.Owner = 'Rah1337' then 1 else 2 end, Withdrawals.ID DESC
`)
withdrawn = 0;
for(x in withdrawals){
if(denomination != 0){
if(withdrawals[x]['Denomination'] > denomination){
withdrawn = withdrawn + denomination;
await db.query(sql`
UPDATE Tenderizer.Withdrawals
SET Denomination = Denomination - ${denomination}
WHERE ID = ${withdrawals[x]['ID']}
`);
await db.query(sql`
INSERT INTO Tenderizer.Points (GenerationTag, Owner, Denomination, BoughtPoint, Since)
VALUES (
${withdrawals[x]['GenerationTag']},
${request.body.custom},
${denomination},
${bsr},
${Date.now()}
)
`);
await db.query(sql`
INSERT INTO Tenderizer.Payouts (GenerationTag, Owner, DollarValue, Denomination, Processed, Since)
VALUES (
${withdrawals[x]['GenerationTag']},
${withdrawals[x]['Owner']},
${bsr * withdrawals[x]['Denomination']},
${denomination},
${0},
${Date.now()}
)
`);
denomination = 0;
}else{
withdrawn = withdrawn + denomination;
await db.query(
sql`DELETE FROM Tenderizer.Withdrawals WHERE ID = ${withdrawals[x]['ID']}`
);
await db.query(sql`
INSERT INTO Tenderizer.Points (GenerationTag, Owner, Denomination, BoughtPoint, Since)
VALUES (
${withdrawals[x]['GenerationTag']},
${request.body.custom},
${withdrawals[x]['Denomination']},
${bsr},
${Date.now()}
)
`);
await db.query(sql`
INSERT INTO Tenderizer.Payouts (GenerationTag, Owner, DollarValue, Denomination, Processed, Since)
VALUES (
${withdrawals[x]['GenerationTag']},
${withdrawals[x]['Owner']},
${bsr * withdrawals[x]['Denomination']},
${withdrawals[x]['Denomination']},
${0},
${Date.now()}
)
`);
denomination = denomination - withdrawals[x]['Denomination'];
}
}
}
if(denomination > 0){
await db.query(sql`
INSERT INTO Tenderizer.Pool (GenerationTag, Username, DollarValue, Withdrawn, Since)
VALUES (
${gt},
${request.body.custom},
${10 - (withdrawn * bsr)},
${0},
${Date.now()}
)
`);
await db.query(sql`
INSERT INTO Tenderizer.Points (GenerationTag, Owner, Denomination, BoughtPoint, Since)
VALUES (
${gt},
${request.body.custom},
${denomination},
${bsr},
${Date.now()}
)
`);
}
await db.query(sql`UPDATE Tenderizer.EHC_Count SET Credits = Credits + ${7 / bsr} WHERE Username = ${request.body.custom}`)
}
};
P.S. it also looks like you're forgetting to actually declare your variables (using let or const before you assign values to them.
P.P.S doing all these queries in a loop may be quite slow, because each one will be executed sequentially. This will be necessary if each query depends on the result of the previous one, but if that's not the case, you could replace one/all of the loops with await Promise.all(list.map(async (value, index) => {...})), which will run the function for each value in the list, but will run it in parallel.

Related

MySQL mysteriously adding VALUES

I have this code in my project
app.post('/history/form/confirm', isLoggedIn, (req,res)=>{
let code = req.body.pcode,
quanti = req.body.qty,
price = req.body.price,
cust = req.body.orderedBy,
oDate = req.body.orderDate;
[code].forEach((product, index, arr) =>{
const q = quanti[index];
let sql = `INSERT INTO inventory.orders (productCode, productName, unitPrice, quantity, totalPrice, customer, date)
VALUES (`+con.escape(product)+`, (SELECT productName FROM inventory.receive WHERE productCode = `+con.escape(product)+`), (SELECT unitPrice FROM inventory.receive WHERE productCode = `+con.escape(product)+`), `+con.escape(q)+`,`+con.escape(price)+`,`+con.escape(cust)+`,`+con.escape(oDate)+`)`
con.query(sql, (err,result)=>{
if (!err){
req.flash('historyMessage', 'Order Created')
res.redirect('/admin/history')
}
else{
res.status(404).send(err);
}
})
})
});
let sql = `INSERT INTO inventory.orders (productCode, productName, unitPrice, quantity, totalPrice, customer, date) VALUES (`+con.escape(product)+`, (SELECT productName FROM inventory.receive WHERE productCode = `+con.escape(product)+`), (SELECT unitPrice FROM inventory.receive WHERE productCode = `+con.escape(product)+`), `+con.escape(q)+`,`+con.escape(price)+`,`+con.escape(cust)+`,`+con.escape(oDate)+`)`
then i get this error
it adds two more columns to VALUES even if I only have 8 columns on my table and the id is on auto increment
What could be the culprit?
I tried other ways of coding like not having a subquery and it still adds those 3 extra values for some reason
HERE is the code from my other project but I didn't use Select because I didnt get the other values from other tables
.post("/send-data", (req,res)=>{
let order = req.body.OrderNo;
let quantity = req.body.quantity;
let first = req.body.fname,
last = req.body.lname,
contact = req.body.Contact,
email = req.body.emailAdd,
fb = req.body.facebook,
date = req.body.date,
delivery = req.body.delivery,
payment = req.body.payment,
time = req.body.time,
address = req.body.address;
[order].forEach((product, index, arr)=>{
const q = quantity[index];
let sql = "INSERT INTO foodorder.orders (" +
"food_id," +
" qty,"+
" customer_FName," +
" customer_LName," +
" customer_address," +
" customer_number," +
" customer_email," +
" customer_facebook," +
" order_date," +
" delivery_option," +
" mode_of_payment," +
" delivery_time" +
") VALUES (" +
con.escape(product) + `,` +
con.escape(q) + `,` +
con.escape(first) + `,` +
con.escape(last) + `,` +
con.escape(address) + `,` +
con.escape(contact) + `,` +
con.escape(""+email) + `,` +
con.escape(fb) + `,` +
con.escape(date) + `,` +
con.escape(delivery) + `,` +
con.escape(payment) + `,` +
con.escape(time) +
`)`;
con.query(sql, (err,result) => {
if(!err){
res.redirect('thankyou.html');
}
else{
res.status(404).send('ERROR. Please Go back and Order Again');
}
})
})
});
For Barmar's Answer
const dbconfig = require('../config/database');
const mysql = require('mysql2');
const con = mysql.createConnection(dbconfig.connection);
con.query('USE ' + dbconfig.database);
module.exports = function(app, passport) {
app.use((req, res, next)=>{
res.locals.filterdata;
next();
})
// LOGIN =========================
// ===============================
app.get('/', (req,res) =>{
res.redirect('/login');
});
app.get('/login', function(req, res) {
res.render(process.cwd() + '/pages/login', { message: req.flash('loginMessage') });
});
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
}),
function(req, res) {
console.log("someone logged in");
if (req.body.remember) {
req.session.cookie.maxAge = 1000 * 60 * 3;
} else {
req.session.cookie.expires = false;
}
res.redirect('/');
});
// FORGOT PW =======================
// =================================
app.get('/forgot', function(req, res) {
res.render(process.cwd() + '/pages/forgot');
});
// PAGE ROUTES =====================
// =================================
app.get('/profile', isLoggedIn, (req, res)=> {
if (req.isAuthenticated() && (req.user.isAdmin === 1)) {
res.redirect('/admin');
}
else{
res.redirect('/cashier');
}
});
// ADMIN ROUTES =====================
// ==================================
app.get('/admin', isLoggedIn, (req,res)=>{
let sql = "SELECT * FROM orders"
con.query(sql, (err,result)=>{
if(!err){
res.render(process.cwd() + '/pages/admin/history', {
data:result,
user: req.user,
message: req.flash('historyMessage')
});
}
else{
res.status(404).send(err);
}
});
});
app.get('/admin/history', isLoggedIn, (req,res)=>{
let sql = "SELECT * FROM orders"
con.query(sql, (err,result)=>{
if(!err){
res.render(process.cwd() + '/pages/admin/history', {
data:result,
user: req.user,
message: req.flash('historyMessage')
});
}
else{
res.status(404).send(err);
}
});
});
app.get('/history/form', isLoggedIn,(req,res)=>{
let sql = "SELECT * FROM receive"
let sql2 = "SELECT * FROM orders"
con.query(sql, (err,result)=>{
con.query(sql2, (err2,result2)=>{
if(!err){
res.render(process.cwd() + '/pages/admin/form', {data2:result2, data:result, user: req.user});
}
else{
res.status(404).send(err, err2);
}
})
});
});
app.post('/history/form/confirm', isLoggedIn, (req,res)=>{
let code = req.body.pcode,
quanti = req.body.qty,
price = req.body.price,
cust = req.body.orderedBy,
oDate = req.body.orderDate;
[code].forEach((product, index, arr) =>{
const q = quanti[index];
let sql = `INSERT INTO inventory.orders (productCode, productName, unitPrice, quantity, totalPrice, customer, date)
SELECT ?, productName, unitPrice, ?, ?, ?, ?
FROM inventory.receive
WHERE productCode = ?`;
console.log(sql);
con.query(sql,[product, q, price, cust, oDate], (err,result)=>{
if (!err){
req.flash('historyMessage', 'Order Created')
res.redirect('/admin/history')
}
else{
console.log(sql);
res.status(404).send(err);
}
})
})
});
app.post('/history/form/confirmPrint', isLoggedIn, (req,res)=>{
let code = req.body.pcode, name = req.body.pname, unit = req.body.punit,
qty = req.body.qty, price = req.body.price, cust = req.body.orderedBy, oDate = req.body.orderDate;
[code].forEach((product, index, arr) =>{
const q = qty[index];
let sql = "INSERT INTO inventory.orders (productCode, productName, unitPrice, quantity, totalPrice, customer, date) VALUES (?,?,?,?,?,?,?)"
con.query(sql,[product, name, unit, q, price, cust, oDate], (err,result)=>{
if (!err){
req.flash('historyMessage', 'Order Created')
res.redirect('/admin/history')
}
else{
res.status(404).send(err);
}
});
})
});
app.get('/admin/stocks', isLoggedIn, (req,res)=>{
let sql = "SELECT * FROM receive"
con.query(sql, (err,result)=>{
if(!err){
res.render(process.cwd() + '/pages/admin/stocks', {data: result, user: req.user});
}
else{
res.status(404).send(err);
}
});
});
app.get('/admin/receive', isLoggedIn, (req,res)=>{
let date = ""+ new Date().getFullYear() + "-" + (new Date().getMonth()+1) + "-" + new Date().getDate() ;
let sql = "SELECT * FROM receive WHERE date = ?";
con.query(sql,[date], (err,result)=>{
if (!err){
req.flash('dateMessage', date)
res.render(process.cwd() + '/pages/admin/receive', {
data: result,
user: req.user,
fltrdate: req.flash('dateMessage'),
message: req.flash('receiveMessage')
});
}
else{
res.status(404).send(err);
}
});
});
app.get('/receive/edit', isLoggedIn, (req,res)=>{
let date = ""+ new Date().getFullYear() + "-" + (new Date().getMonth()+1) + "-" + new Date().getDate() ;
let sql = "SELECT * FROM receive WHERE date = ?";
con.query(sql,[date], (err,result)=>{
if (!err){
req.flash('dateMessage', "" + date)
res.render(process.cwd() + '/pages/admin/editReceive', {
data: result,
user: req.user,
fltrdate: req.flash('dateMessage')
});
}
else{
res.status(404).send(err);
}
});
});
app.post('/receive/edit/delete', isLoggedIn, (req,res)=>{
let date = ""+ new Date().getFullYear() + "-" + (new Date().getMonth()+1) + "-" + new Date().getDate() ;
let sql = "DELETE FROM receive WHERE (date,productCode) = (?,?)";
con.query(sql,[date, req.body.deleteProd], (err,result)=>{
if (!err){
req.flash('receiveMessage', 'Successfully deleted')
res.redirect('/admin/receive')
}
else{
res.status(404).send(err);
}
});
});
app.post('/receive/edit/save', isLoggedIn, (req,res)=>{
let date = ""+ new Date().getFullYear() + "-" + (new Date().getMonth()+1) + "-" + new Date().getDate() ;
let code = req.body.code; let product = req.body.product;
let unit = req.body.unit; let quantity = req.body.quantity;
[code].forEach((p, index, arr)=>{
const q = quantity[index];
let sql = "INSERT INTO inventory.receive (productName, unitPrice, quantity, date) VALUES (?,?,?,?)";
con.query(sql,[product, unit, q, date], (err,result)=>{
if (!err){
req.flash('receiveMessage', 'Successfully saved')
res.redirect('/admin/receive')
}
else{
res.status(404).send(err);
}
});
});
});
// FILTER ADMIN ROUTES =====================
// =========================================
app.post('/receive/filter', isLoggedIn, (req,res)=>{
let date2 = req.body.date;
filterdata = date2;
let sql = "SELECT * FROM receive WHERE date = ?";
con.query(sql,[date2], (err,result)=>{
if (!err){
req.flash('dateMessage', date2)
res.render(process.cwd() + '/pages/admin/receiveFltr', {
data: result,
user: req.user,
message: req.flash('receiveMessage'),
fltrdate: req.flash('dateMessage')
});
}
else{
res.status(404).send(err);
}
});
});
app.post('/filter/edit', isLoggedIn, (req,res)=>{
let date3 = filterdata;
let sql = "SELECT * FROM receive WHERE date = ?";
con.query(sql,[date3], (err,result)=>{
if (!err){
req.flash('dateMessage', date3)
res.render(process.cwd() + '/pages/admin/editReceiveFltr', {
data: result,
user: req.user,
fltrdate: req.flash('dateMessage')
});
}
else{
res.status(404).send(err);
}
});
});
app.post('/filter/edit/delete', isLoggedIn, (req,res)=>{
let date = filterdata;
let sql = "DELETE FROM receive WHERE (date,productCode) = (?,?)";
con.query(sql,[date, req.body.deleteProd], (err,result)=>{
if (!err){
req.flash('receiveMessage', 'Successfully deleted')
res.redirect('/admin/receive')
}
else{
res.status(404).send(err);
}
});
});
app.post('/filter/edit/save', isLoggedIn, (req,res)=>{
let date = filterdata;
let code = req.body.code; let product = req.body.product;
let unit = req.body.unit; let quantity = req.body.quantity;
[code].forEach((p, index, arr)=>{
const q = quantity[index];
let sql = "INSERT INTO inventory.receive (productName, unitPrice, quantity, date) VALUES (?,?,?,?)";
con.query(sql,[product, unit, q, date], (err,result)=>{
if (!err){
req.flash('receiveMessage', 'Successfully saved')
res.redirect('/admin/receive')
}
else{
res.status(404).send(err);
}
});
});
});
// CASHIER ROUTES =====================
// =================================
// LOGOUT =========================
// ================================
app.get('/logout', (req, res)=> {
req.logout();
res.redirect('/login');
});
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
}
}
I'm not sure where the extra values are coming from, but you can simplify this by using a prepared statement with parameters. And the query can use INSERT INTO ... SELECT ... rather than putting subqueries into the VALUES list.
app.post('/history/form/confirm', isLoggedIn, (req,res)=>{
let code = req.body.pcode,
quanti = req.body.qty,
price = req.body.price,
cust = req.body.orderedBy,
oDate = req.body.orderDate;
[code].forEach(product => {
let sql = `INSERT INTO inventory.orders (productCode, productName, unitPrice, quantity, totalPrice, customer, date)
SELECT ?, productName, unitPrice, ?, ?, ?, ?
FROM inventory.receive
WHERE productCode = ?`;
con.query(sql, [product, q, price, cust, oDate, product], (err,result)=>{
if (!err){
req.flash('historyMessage', 'Order Created')
res.redirect('/admin/history')
}
else{
res.status(404).send(err);
}
});
});
});
I have solved the issue and it's in my EJS file. I have an iteration to view the results from my table and have inputs for the price. The price didn't have a disabled attribute that's why it keeps accepting the other prices even if the checkbox is false.
Just add disabled and create a JS file that removes the disabled attribute if the checkbox is checked == true.

Unable to get insertId of the last INSERT in MySQL with NodeJS

I'm using MySQL with NodeJS with asyncs and awaits. I'm trying to get the last insertid from my inserted row but keep getting errors.
Here's the async function;
function makeDb( config ) {
const connection = mysql.createConnection( config ); return {
query( sql, args ) {
return util.promisify( connection.query )
.call( connection, sql, args );
},
close() {
return util.promisify( connection.end ).call( connection );
}
};
}
And here's the code which is failing on the queries;
try {
if(tag1){
row_b = await db.query( "SELECT tagid FROM tags WHERE tagname = ?", [tag1]);
const onetagid1 = row_b[0].tagid;
console.log('onetagid1 = ' + onetagid1);
if (row_b > 0){
row_c = await db.query("
INSERT INTO entitytag (tagid1, audioid) VALUES (?,?)
ON DUPLICATE KEY UPDATE tagid1 = ?" [onetagid1, audioid, onetagid1]
);
} else {
row_d = await db.query( 'INSERT IGNORE INTO tags (tagname) VALUES (?)', [tag1]);
var twotagid1 = row_d.insertId;
console.log('twotagid1 2nd = ' + twotagid1);
row_e = await db.query(
"INSERT INTO entitytag (tagid1, audioid) VALUES (?,?)
ON DUPLICATE KEY UPDATE tagid1 = ?" [twotagid1, audioid, twotagid1]
);
}
res.json('json success!');
}
}
And here's the error;
onetagid1 = 30
twotagid1 2nd = 0
ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'I' at line 1
The error is twotagid1 2nd = 0 which should not be zero.
I'm not sure why this works when the other didn't. But I'll post it here hoping someone will be able to spot it;
try {
if(tag1){
row_c = await db.query( "SELECT tagid FROM tags WHERE tagname = ?", [tag1]);
if (row_c.length > 0){
console.log('tag exists in database ');
const tagid1 = row_c[0].tagid;
console.log('tagid1 = ' + tagid1);
row_f = await db.query(
"INSERT INTO entitytag (tagid1, audioid) VALUES (?,?)
ON DUPLICATE KEY UPDATE tagid1 = ?", [tagid1, audioid, tagid1 ]);
} else {
console.log('tag does not exist in database ');
row_d = await db.query( 'INSERT IGNORE INTO tags (tagname) VALUES (?)', [tag1]);
const tagInsertId = row_d.insertId;
console.log('tagInsertId = ' + tagInsertId);
row_e = db.query(
'INSERT INTO entitytag (tagid1, audioid) VALUES (?,?)
ON DUPLICATE KEY UPDATE tagid1 = ?', [tagInsertId, audioid, tagInsertId ]);
}
}
console.log('success!');
res.json(tag1);
}

Node.js chaining promises in a loop with MySQL

I am trying to make a non-relational DB into a relational DB. So I am starting from data with no unique IDs.
I need to get the result from one SQL call loop through those rows, and for each one, do a SQL SELECT using part of the first result, then another SQL select using the next result, and then a write using IDs from the first and last queries.
I am using Node.js and ES6 promises to keep everything in order, but I seem to be missing something. I was actually trying to do an extra SQL call, and also use that result in the third query, but I am simplifying it to just get one call to feed into another.
Maybe some code will help show what I am trying to do.
Here is my query class that returns promises:
var mysql = require('mysql');
class Database {
constructor() {
this.connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "pressfile"
});
}
query(sql, args) {
return new Promise((resolve, reject) => {
this.connection.query(sql, args, (err, result, fields) => {
if (err) return reject(err);
resolve (result);
});
});
}
close() {
return new Promise((resolve, reject) => {
this.connection.end(err => {
if (err) return reject (err);
resolve();
});
});
}
}
This was stolen pretty much as is from a tutorial site, and this part seems to work pretty well. Then here comes the loop, and the multiple queries:
var contactId;
var address1;
var orgName;
var database = new Database();
database.query("SELECT * FROM contact")
.then( result => {
for (var i = 0; i < result.length; i++) {
contactId = result[i].contactId;
orgName = result[i].org;
var sql2 = "SELECT * FROM organization WHERE (name = \"" + orgName + "\")";
console.log(sql2);
database.query(sql2)
.then(result2 => {
console.log(result2);
var orgId = result2[0].organizationId;
var sql3 = "INSERT INTO contact_organization (contactId, organizationId) VALUES (" + contactId + ", " + orgId + ")";
console.log(sql3);
return ""; //database.query(sql3);
}).then( result3 => {
console.log(result3);
});
}
}).catch((err) => {
console.log(err);
databse.close();
});
I know it is kind of unraveling at the end, but I'm not wanting to do the INSERT query until I know I can get it right. Right now in the console, I get a valid organization object, followed by:
`INSERT INTO contact_organization (contactId, organizationId) VALUES (17848, 29)'
17848 is the final contactId that is returned in the for loop. How can I get the contactId that is assigned before the second query. I know I am not doing this asynchronous stuff right.
Try something like this. Just a quick solution. (not tested).
const selectOrg = (result) => {
contactId = result[i].contactId;
orgName = result[i].org;
var sql = "SELECT * FROM organization WHERE (name = \"" + orgName + "\")";
return database.query(sql);
};
const insertOrg = (result) => {
var orgId = result[0].organizationId;
var sql = "INSERT INTO contact_organization (contactId, organizationId) VALUES (" + contactId + ", " + orgId + ")";
return database.query(sql);
};
database.query("SELECT * FROM contact")
.then(result => {
const promises = [];
for (var i = 0; i < result.length; i++) {
promises << selectOrg(result)
.then(insertOrg);
}
return Promise.all(promises);
})
.then(allResults => {
console.log(allResults);
})
.catch((err) => {
databse.close();
});
I found a way to do this, but it is kind of cheesy. I included the contactId as a constant in the SQL query to get the organization, so I could then pass the value to the .then, keeping everything in order.
My sql2 statement becomes:
var sql2 = "SELECT *, " + contactId + " AS contactId FROM organization WHERE (name = \"" + orgName + "\")";
Then when that query returns, I can just pull the correct contactId out as result[0].contactId, from the same result I get the organizationId from.
Here is the final code:
database.query("SELECT * FROM contact")
.then( result => {
for (var i = 0; i < result.length; i++) {
var contactId = result[i].contactId;
var orgName = result[i].org;
var sql2 = "SELECT *, " + contactId + " AS contactId FROM organization WHERE (name = \"" + orgName + "\")";
database.query(sql2)
.then(result2 => {
var orgId = result2[0].organizationId;
var contactId = result2[0].contactId;
var sql3 = "INSERT INTO contact_organization (contactId, organizationId) VALUES (" + contactId + ", " + orgId + ")";
console.log(sql3);
return database.query(sql3);
}).then( result3 => {
console.log(result3);
});
}
}).catch((err) => {
console.log(err);
databse.close();
});
The console.log(result3) returns a bunch of these:
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 }
And I got one contact_organization inserted for every contact row returned from the first query.

perform async multiple Mysql queries on Node

I'm using node with Mysql and here's my problem.
I'm trying to add new photos on my database and return it as an array
here is my function :
function addNewPhotos(_id, files) {
var deferred = Q.defer();
var new_photos = []
_.each(files, function (one) {
var data = [
one.path,
_id,
0
]
var sql = 'INSERT INTO photos(photo_link, id_user, isProfil) VALUES (?, ?, ?)';
db.connection.query(sql, data, function (err, result) {
if (err)
deferred.reject(err.name + ': ' + err.message);
var sql = 'SELECT id_user, photo_link, isProfil FROM `photos` WHERE id = ?';
if (result){
db.connection.query(sql, [result.insertId], function(err, photo) {
if (err) deferred.reject(err.name + ': ' + err.message);
if (photo) {
new_photos.push(photo[0]);
}
});
}
})
})
deferred.resolve(Array.prototype.slice.call(new_photos));
return deferred.promise}
The Insert works well but i can't retrieve the results to send them back to the client. (my array is empty)
Thanks.
Always promisify at the lowest level, in this case db.connection.query().
if(!db.connection.queryAsync) {
db.connection.queryAsync = function(sql, data) {
return Q.Promise(function(resolve, reject) { // or possibly Q.promise (with lower case p), depending on version
db.connection.query(sql, data, function(err, result) {
if(err) {
reject(err);
} else {
resolve(result);
}
});
});
};
}
Now the higher level code becomes very simple :
function addNewPhotos(_id, files) {
var sql_1 = 'INSERT INTO photos(photo_link, id_user, isProfil) VALUES (?, ?, ?)',
sql_2 = 'SELECT id_user, photo_link, isProfil FROM `photos` WHERE id = ?';
return Q.all(files.map(function(one) {
return db.connection.queryAsync(sql_1, [one.path, _id, 0]).then(function(result) {
return db.connection.queryAsync(sql_2, [result.insertId]);
});
}));
};
To prevent a single failure scuppering the whole thing, you might choose to catch individual errors and inject some kind of default ;
function addNewPhotos(_id, files) {
var sql_1 = 'INSERT INTO photos(photo_link, id_user, isProfil) VALUES (?, ?, ?)',
sql_2 = 'SELECT id_user, photo_link, isProfil FROM `photos` WHERE id = ?',
defaultPhoto = /* whatever you want as a default string/object in case of error */;
return Q.all(files.map(function(one) {
return db.connection.queryAsync(sql_1, [one.path, _id, 0]).then(function(result) {
return db.connection.queryAsync(sql_2, [result.insertId]);
}).catch(function() {
return defaultPhoto;
});
}));
};
Do the return in your async loop function when all has been done
function addNewPhotos(_id, files) {
var deferred = Q.defer();
var new_photos = [];
var todo = files.length;
var done = 0;
_.each(files, function (one) {
var data = [
one.path,
_id,
0
]
var sql = 'INSERT INTO photos(photo_link, id_user, isProfil) VALUES (?, ?, ?)';
db.connection.query(sql, data, function (err, result) {
if (err)
deferred.reject(err.name + ': ' + err.message);
var sql = 'SELECT id_user, photo_link, isProfil FROM `photos` WHERE id = ?';
if (result){
db.connection.query(sql, [result.insertId], function(err, photo) {
if (err) deferred.reject(err.name + ': ' + err.message);
if (photo) {
new_photos.push(photo[0]);
}
if(++done >= todo){
deferred.resolve(Array.prototype.slice.call(new_photos));
return deferred.promise
}
});
}
else
{
if(++done >= todo){
deferred.resolve(Array.prototype.slice.call(new_photos));
return deferred.promise;
}
}
})
})
}

Passing a key-value array in NodeJS for MySQL select query

I can pass an array to an mysql insert in nodeJS like so..
var data = {userId: 3, name: "sample"}
db.query('insert into my_table SET ?', data, function(err, result){...}
Is there a similar way of passing an array to a select query in the where clause... without specifying all the fields?
var data = {userId: 3, name: "sample"}
db.query('select * from my_table WHERE ?', data, function(err, result){...}
Doesn't seem to work.. nor does using the SET name in place of where...
database.conn.config.defaultQueryFormat = function (query, values) {
if (!values) return query;
var updatedQuery = query.replace("?", function () {
var whereClause = "";
for(var index in values){
whereClause += mysql.escapeId(index) + " = " + db.escape(values[index]) + " and ";
}
whereClause = whereClause.substring(0, whereClause.length - 5);
return whereClause;
});
return updatedQuery;
};
This appears to work.. e.g.
var val = db.query('select * from my_table where ?', data, function(err, result) {
}