“update query in promise not running” 可以翻译为 “在Promise中未运行的更新查询”。

huangapple go评论74阅读模式
英文:

update query in promise not running

问题

请帮助,更新查询在我的Promise中没有运行。
它直接跳过到最后一条语句"done with six"。数据库表中应该更新的行没有被更新。我如何让更新查询成功运行?

1. 我放在更新语句上面的CRUD语句(包括选择和插入语句)

code would be here

2. 似乎没有运行的更新语句

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. Promise语句(允许CRUD语句同步运行,因为这些语句彼此依赖)

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) //没有运行这部分代码
  .then((res) => {
    console.log("done with six");
    return res;
  });

4. 终端或控制台中显示的结果

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
英文:

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

答案1

得分: 1

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) // 不运行这段代码区域

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);
})
})
英文:

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

huangapple
  • 本文由 发表于 2023年1月4日 19:58:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75005118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定