gpt4 book ai didi

javascript - jQuery 以错误的顺序执行代码部分

转载 作者:行者123 更新时间:2023-12-03 12:37:16 24 4
gpt4 key购买 nike

我目前正在为我的叔叔做一个项目,他是一名 vert 。这是一个 Electron 项目,让他可以管理他的客户、病人和他们的访问。然而,在使用一段时间后,我们意识到,有时 ipcRenderer 会在尝试插入新客户、患者或约会时触发两次,甚至更多次。我试图通过添加另一个 sql 查询来捕获这一点,该查询检查是否有相同的记录,我想要插入的内容已经在数据库中。但是现在,每当我尝试插入新记录时, db.all() (起初是 db.run() ,在过去的几个小时里我一直在尝试通过反复试验来修复并且绝望了)哪个插入记录在 db.all() 之前执行,它获取相同记录的数量并设置计数器,尽管它应该在之后执行。我尝试使用 $.when() 和 .done() 来解决这个问题,但是遇到了无法检查计数器是否为 0 的问题,因为 .done() 需要一个函数,而 .done(如果) 不起作用。顺便说一句,我更像是一个 C++、java、C# 的家伙,并且对 jQuery、JS 和其他东西不太熟悉,如果我的错误是微不足道的,那么抱歉。
这是代码,你可以忽略这些长长的 SQL 语句,我知道,它们不符合编码约定,但它们有效:

$("#btn-ins-patient").on("click", function() {
ipcRenderer.once('insert-patient', function(event, arg) {

let checksql = "select count(*) as 'zaehl' from patient where "
+ "owner = " + arg.owner + " and name = '" + arg.name + "' and species = '" + arg.species + "' and breed = '" + arg.breed + "' and sex = '" + arg.sex + "' and neutered = '" + arg.neutered + "' and dead = '" + arg.dead + "' and chipnr = '" + arg.chipnr + "' and birthdate = '" + arg.birthdate + "' and color = '" + arg.color + "';"

db.all(checksql, function(error, rows) {
if (error) {
console.log(error.code, checksql)
insertpatsql = "select * from patient where id = 1;";
}
else{
console.log(checksql); //second output in console, prints the right statement
for (let i = 0; i < rows.length; i++) {
var counter = rows[i].zaehl;
console.log(counter); //third output in console, prints 0
setCount(counter);
console.log(cnter); //fourth output in console, prints 0 again
}
if(cnter != 0){
insertpatsql = "select * from patient where id = 1;";
console.log(insertpatsql);
console.log("double entry was stopped");
}
else{

console.log(insertpatsql); // fifth output in console prints the right statement (insert)

}

}
});

let insertpatsql = "insert into patient"
+ "(owner, name, species, breed, sex, neutered, dead, chipnr, birthdate, color, notes, diverse)"
+ "values ('" + arg.owner + "', '" + arg.name + "', '" + arg.species + "', '" + arg.breed + "', '" + arg.sex + "', '" + arg.neutered + "', '" + arg.dead + "', '" + arg.chipnr + "', '" + arg.birthdate + "', '" + arg.color + "', '" + arg.notes + "', '" + arg.diverse + "');";

if(cnter == 0){
db.all(insertpatsql, function(error) {
if (error) {
console.log(error.code, insertpatsql)
}
else{
console.log(insertpatsql);
loadPatients();
}
});
}
else{
console.log("double entry was tried", insertpatsql, cnter);
//first output in console, cnter is undefined
}
})

let child = new BrowserWindow({
height: 485,
width: 795,
parent: parent,
modal: true,
resizable: false,
show: false,
webPreferences: {
nodeIntegration: true
}
})

child.loadFile('./ins/patient.html')

child.once('ready-to-show', () => {
child.show()
})

child.on('closed', () => {
child = null
})
})
select * from patient where id = 1;只是出于安全原因,因为有时会执行第二个 db.all() ,即使它不应该执行。我知道我的代码很乱,对不起,我整天都在努力解决这个问题......
这就是我设置cnter(全局变量)的方式:
var cnter;

function setCount(cnt){
cnter = cnt;
}
如何确保 checksql 及其代码块在插入之前执行?

最佳答案

好的,我想我通过将第二个 block 放在第一个 block 内来解决它。这是现在的代码:

$("#btn-ins-patient").on("click", function() {
ipcRenderer.once('insert-patient', function(event, arg) {

let checksql = "select count(*) as 'zaehl' from patient where "
+ "owner = " + arg.owner + " and name = '" + arg.name + "' and species = '" + arg.species + "' and breed = '" + arg.breed + "' and sex = '" + arg.sex + "' and neutered = '" + arg.neutered + "' and dead = '" + arg.dead + "' and chipnr = '" + arg.chipnr + "' and birthdate = '" + arg.birthdate + "' and color = '" + arg.color + "';"

let insertpatsql = "insert into patient"
+ "(owner, name, species, breed, sex, neutered, dead, chipnr, birthdate, color, notes, diverse)"
+ "values ('" + arg.owner + "', '" + arg.name + "', '" + arg.species + "', '" + arg.breed + "', '" + arg.sex + "', '" + arg.neutered + "', '" + arg.dead + "', '" + arg.chipnr + "', '" + arg.birthdate + "', '" + arg.color + "', '" + arg.notes + "', '" + arg.diverse + "');";


db.all(checksql, function(error, rows) {
if (error) {
console.log(error.code, checksql)
insertpatsql = "select * from patient where id = 1;";
}
else{
console.log(checksql); //first output in console
for (let i = 0; i < rows.length; i++) {
var counter = rows[i].zaehl;
console.log(counter); //second output in console
setCount(counter);
console.log(cnter); //third output in console
}
if(cnter != 0){
insertpatsql = "select * from patient where id = 1;";
console.log(insertpatsql);
console.log("double entry was stopped");
}
else{

if(cnter == 0){
db.all(insertpatsql, function(error) {
if (error) {
console.log(error.code, insertpatsql)
}
else{
console.log(insertpatsql);
loadPatients();
}
});
}
else{
console.log("double entry was tried", insertpatsql, cnter); //fourth output in console, cnter is undefinded
}

}

}
});

})
})
我不知道它是否真的有效,直到 ipcRenderer 尝试再次触发两次。一旦我确认它有效,我会标记它。

关于javascript - jQuery 以错误的顺序执行代码部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66186690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com