update query in promise not running - mysql

please help, the update query isn't running in my promise.
It just skips to the last statement "done with six". The row that it's meant to update in the database table doesn't get updated. how can I make the update query run successfully?
1. crud statements(select+insert statements) that I've placed above the update statement
code would be here
2. update statement that does not seem to run
var insertcctblintblbotagents = await new Promise((resolve, reject) => {
var sql='UPDATE db.tblagentstoadd SET db.tblagentstoadd.ccAgentID =? WHERE
db.agentstoadd.AgentID=? ;';
DB.query(sql,[ctx.session.AgentID, ctx.session.tempAgentID],function(err,result){
if (err){
return reject(err);
};
return resolve(result);
})
})
3. the promise statement (allows the crud statements to run synchronously because the statements are dependent on one another)
await insertbotagentstoadd
.then(() => {
console.log("done with one");
})
.then(() => selectbotagentstoadd)
.then((results) => {
AgenttoaddIDStore = [];
results.forEach((agent) => {
AgenttoaddIDStore.push({
AgentID: agent.AgentID,
});
ctx.session.tempAgentID = agent.AgentID;
});
return AgenttoaddIDStore;
})
.then((res) => {
console.log("agent ID: "+ctx.session.tempAgentID);
console.log("done with two");
return res;
})
.then((results) => insertcctblricaagents)
.then((res) => {
console.log("done with three");
return res;
})
.then((results) => selectcctblricaagents)
.then((res) => {
console.log("done with four");
return res;
})
.then((res)=>selectcctblricaagentsnum)
.then((result)=>{
AgentNewIDStore=[];
result.forEach((agent)=>{
AgentNewIDStore.push({
AgentID:agent.AgentID,
MainNumber:agent.MainNumber,
});
ctx.session.AgentID=agent.AgentID;
ctx.session.agentnumber=agent.MainNumber;
});
return AgentNewIDStore;
})
.then((res)=>{
console.log("cctblricaagentsnum agent ID: "+ ctx.session.AgentID);
console.log("done with five");
return res;
})
.then((result)=>insertcctblintblbotagents) //Doesn't run this area of code
.then((res)=>{
console.log("done with six");
return res;
});
4.results displayed in the terminal or console
done with one
agent ID: 151
done with two
done with three
done with four
cctblricaagentsnum agent ID: 96661
done with five
done with six

It does run the query, but it runs before you intend it to... You execute the query when you define the promise, not when you "use" it. The code is looking weird so I won't redo everything, but I suggest you use awaits instead of a chain of then(), it will make things more readable. If you inline the promise you defined, things will work:
.then((result)=>insertcctblintblbotagents) //Doesn't run this area of code
To
.then((result)=>{
return new Promise((resolve, reject) => {
var sql='UPDATE db.tblagentstoadd SET db.tblagentstoadd.ccAgentID =? WHERE
db.agentstoadd.AgentID=? ;';
DB.query(sql,[ctx.session.AgentID, ctx.session.tempAgentID],function(err,result){
if (err){
return reject(err);
};
return resolve(result);
})
})

Related

Return mysql query result using Promise

I'm currently a little confused as to how to properly wait for the promise to finish before returning the result from a query
Here is my current code:
const getLeaderboardValues = async () => {
const SQLConnection = await getSQLConnection();
return new Promise((resolve, reject) => {
SQLConnection.query(getValuesSQLQuery, (err, result) => {
if (err) { reject(err) }
return resolve(result);
});
SQLConnection.end()
})
}
const runtime = () => {
getLeaderboardValues().then((result) => {
console.log(result);
})
}
The code above does log the correct results while debbugging, i believe this is because i'm giving the code more time to render with the breakpoints, however when running normally i get undefined
I believe the SQLConnection.end() line is executing before the query is returned, given it is outside the query statement.
The below may solve your issue, however I do not recommend opening a connection and closing it on every request in production systems.
const getLeaderboardValues = async () => {
const SQLConnection = await getSQLConnection();
return new Promise((resolve, reject) => {
SQLConnection.query(getValuesSQLQuery, (err, result) => {
if (err) {
reject(err)
return SQLConnection.end()
}
SQLConnection.end()
return resolve(result);
});
})
}
const runtime = () => {
getLeaderboardValues().then((result) => {
console.log(result);
})
}

Executing different batch inserting into one transaction in Nodejs

I need to execute two different inserts for two different collections, one depends on the other one, so I need to execute the first one and then being able to execute the second one. My problem is I need to be able to roll back all operations if some error happens so I need to roll back both inserts if an error occurs.
I'm trying to do something like this with my two batch calls inside of the transactions but it doesn't work.
conn.beginTransaction()
.then(() => {
conn.query("INSERT INTO testTransaction values ('test')");
return conn.query("INSERT INTO testTransaction values ('test2')");
//instead of this two query I have my batch inserts
})
.then(() => {
conn.commit();
})
.catch((err) => {
conn.rollback();
})
Any example of how to do it?
Just if someone has the same situation and what to know how I figure this out, here is a working code:
return connectionMaria.beginTransaction()
.then(() => {
return conn.batch(insert1, values1)
.then(() => {
return conn.batch(insert2, values2)
})
.then(() => {
connectionMaria.commit();
})
.catch((error) => {
connectionMaria.rollback();
throw error;
});
If you have lots of promises you need to wait on, you can use Promise.all:
conn.beginTransaction()
.then(() => {
return Promise.all([
conn.query("INSERT INTO testTransaction values ('test')"),
conn.query("INSERT INTO testTransaction values ('test2')")
])
})
.then(() => {
conn.commit();
})
.catch((err) => {
conn.rollback();
})

MySQL NodeJS .then() s not a function

Can't I use promise for nodeJS mysql query?
// My DB settings
const db = require('../util/database');
db.query(sqlQuery, [param1, param2])
.then(result => {
console.log(result);
})
.catch(err => {
throw err;
});
It is returning: TypeError: db.query(...).then is not a function
You mentioned in the comments that you want logic after the query block to be awaited, without placing that logic inside of the callback. By wrapping the method with a Promise, you can do that as such:
try {
const result = await new Promise((resolve, reject) => {
db.query(sqlQuery, (error, results, fields) => {
if (error) return reject(error);
return resolve(results);
});
});
//do stuff with result
} catch (err) {
//query threw an error
}
Something like this should work
function runQuery(sqlQuery){
return new Promise(function (resolve, reject) {
db.query(sqlQuery, function(error, results, fields) {
if (error) reject(error);
else resolve(results);
});
});
}
// test
runQuery(sqlQuery)
.then(function(results) {
console.log(results)
})
.catch(function(error) {
throw error;
});
mysql package does not support promise. We can use then only a function call returns a promise.You can use mysql2 which has inbuilt support for Promise. It will also make your code more readable. From mysql2 docs:
async function main() {
// get the client
const mysql = require('mysql2/promise');
// create the connection
const connection = await mysql.createConnection({host:'localhost',
user: 'root', database: 'test'});
// query database
const [rows, fields] = await connection.execute(query);
// rows hold the result
}
I would aslo recommend you to learn about callbacks, promise and async-await

insert in for each with async await

error on query execution
var records = [{name:'John',age:24},{name:'Sarah',age:28},{name:'Linda',age:23}];
connection.getSession().then(session => {
async function insertRecords() {
await Promise.all(records.map(async function (element) {
let util = {};
util['name'] = element['name'];
util['age'] = element['age'] || null;
let query = `INSERT INTO users SET
name =?,
age=?
ON DUPLICATE KEY UPDATE
name=VALUES(name),
age=VALUES(age)`;
session.sql(query).bind([util.name, util.age]).execute()
.then(() => {})
.catch((error) => {
console.error('cannot execute the query');
console.error('error', error);
});
}))
}
insertRecords()
.then(data => console.log('data', data))
.catch(err => console.log('err', err))
}).catch((err) => {
console.error(err);
session.close();
});
I am getting the following error on execution.
{ severity: 0,
code: 5015,
sqlState: 'HY000',
msg: 'Too many arguments' } }
In your Promise.all, you map on records but your resolver function doesn't return anything. You would need to return a promise. To do so, you should just replace your session.sql block with the following: return session.sql(query).bind([util.name, util.age]).execute().
That would return an array of Promises to Promise.all, and it would work. The error handling is already done when you call your insertRecords function so you don't need to worry about it in your resolver.

Stop another .then() execution in chain of promises

I have programme in nodejs & mysql like below
db.query()
.then(1)
.then(2)
.then(3)
.catch()
I am checking a value from database in then(1) and trying to return response from there.In then(2) , I am executing another code that uses some data from result of then(1) and so on..
My problem: When returning response from then(1), catch() is calling(because then(2) have error, not getting data from then(1)) . So is there any way I can stop further execution in then(1) so that then(2) and catch() couldn't call ?
db.query('query......', [val1, val2])
.then(rslt => { return res.json({ mssg: "Email already exists!", error: "Email already exists!" }) })
.then(user => { return db.query('INSERT INTO ', value, (err, res, flds) => { err ? reject(err) : resolve(res) }) })
.then(user => { return res.json({ mssg: "Success", success: true}) })
.catch( (err) => { console.log(err) })
You can (and should) use an async function, instead of using the lower-level .then() API of the Promise object:
async function doTheThing() {
try {
const result = await db.query('...');
if (result) { // user exists
return res.json({...}); // this will end the entire function
}
const user = await db.query('...');
return res.json({...}); // success
} catch (err) {
console.log(err); // I don't recommend doing this. try/catch should be for recovery
}
}