Home > Enterprise >  update query in promise not running
update query in promise not running

Time:01-05

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

CodePudding user response:

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