我有一个函数,它查询我的数据库,然后调用getNumOfSessionForTrainerClientList
函数:
var select = "select * from USERS WHERE ASSIGNED_TRAINER = ?"
mysqlconn.connect(function(err) {
if (err) {
console.error('Database connection failed: ' + err.stack);
return;
}
mysqlconn.query(select, [req.session.username], async function(err, rows) {
if (err) {
console.log(err);
} else {
let numOfSessionsLeft = {
numberOfSessionsLeftIs: 0
}
for (var i = 0; i < rows.length; i++) {
getNumOfSessionForTrainerClientList(req, res, rows[i], numOfSessionsLeft)
rows[i]["NUM_OF_SESSIONS_PER_MONTH"] = (parseInt(rows[i]["NUM_OF_SESSIONS_PER_MONTH"]) - numOfSessionsLeft.numberOfSessionsLeftIs)
console.log(numOfSessionsLeft)
}
}
mysqlconn.end();
})
})
然后在函数getNumOfSessionForTrainerClientList
中,我得到了这个:
async function getNumOfSessionForTrainerClientList(req, res, rows, numOfSessionsLeft) {
var getNumOfSessionForTrainerClientList = "select * from SCHEDULE WHERE CLIENT_USERNAME = ? AND ASSIGNED_TRAINER = ?"
mysqlconn.connect(async function(err) {
if (err) {
console.error('Database connection failed: ' + err.stack);
return;
}
mysqlconn.query(getNumOfSessionForTrainerClientList, [rows["USERNAME"], req.session.username], async function(err, sessionData) {
if (err) {
console.log(err);
} else {
numOfSessionsLeft.numberOfSessionsLeftIs = 1;
console.log(numOfSessionsLeft.numberOfSessionsLeftIs)
}
})
})
}
然而,发生的情况是,这一行:rows[i]["NUM_OF_SESSIONS_PER_MONTH"] = (parseInt(rows[i]["NUM_OF_SESSIONS_PER_MONTH"]) - numOfSessionsLeft.numberOfSessionsLeftIs)
实际上是在将0
分配给rows[i]["NUM_OF_SESSIONS_PER_MONTH"]
,因为变量分配是在getNumOfSessionForTrainerClientList
的函数调用完成之前发生的。所以这是不同步的。我不知道如何解决这个问题,我以前遇到过async
await
的问题,但没有这样的问题。非常感谢您的帮助。
For循环本质上是同步的,因此不需要等待方法getNumOfSessionForTrainerClientList
返回来继续迭代。
正确的做法是:
- 将代码放入异步块中,并在调用
getNumOfSessionForTrainerClientList
调用时使用await(( - 其次,您的函数
getNumOfSessionForTrainerClientList
应该回报承诺
样品:
async function functionA() {
var select = "select * from USERS WHERE ASSIGNED_TRAINER = ?"
mysqlconn.connect(function(err) {
if (err) {
console.error('Database connection failed: ' + err.stack);
return;
}
mysqlconn.query(select, [req.session.username], async function(err, rows) {
if (err) {
console.log(err);
} else {
let numOfSessionsLeft = {
numberOfSessionsLeftIs: 0
}
for (var i = 0; i < rows.length; i++) {
await getNumOfSessionForTrainerClientList(req, res, rows[i], numOfSessionsLeft)
rows[i]["NUM_OF_SESSIONS_PER_MONTH"] = (parseInt(rows[i]["NUM_OF_SESSIONS_PER_MONTH"]) - numOfSessionsLeft.numberOfSessionsLeftIs)
console.log(numOfSessionsLeft)
}
}
mysqlconn.end();
})
})
}
您的函数getNumOfSessionForTrainerClientList应该看起来像:
async function getNumOfSessionForTrainerClientList(req, res, rows, numOfSessionsLeft) {
return new Promise((resolve, reject) => {
var getNumOfSessionForTrainerClientList = "select * from SCHEDULE WHERE CLIENT_USERNAME = ? AND ASSIGNED_TRAINER = ?"
mysqlconn.connect(async function(err) {
if (err) {
console.error('Database connection failed: ' + err.stack);
return reject(err);
}
mysqlconn.query(getNumOfSessionForTrainerClientList, [rows["USERNAME"], req.session.username], async function(err, sessionData) {
if (err) {
console.log(err);
return reject(err);
} else {
numOfSessionsLeft.numberOfSessionsLeftIs = 1;
console.log(numOfSessionsLeft.numberOfSessionsLeftIs)
return resolve();
}
})
})
})
}
然后用你需要的任何参数调用functionA()
。
您调用包含异步代码的函数,因此getNumOfSessionForTrainerClientList不依赖于上面的代码(以(rows[i][quot;NUM_OF and console(开头(您应该做的是
-
通过返回prom 使函数异步
-
使用等待关键字执行
async function trainerClientList(){ return new Promise((resolve,reject)=>{ var getNumOfSessionForTrainerClientList = "select * from SCHEDULE WHERE CLIENT_USERNAME = ? AND ASSIGNED_TRAINER = ?" mysqlconn.connect(async function(err) { if (err) { console.error('Database connection failed: ' + err.stack); resolve() } mysqlconn.query(getNumOfSessionForTrainerClientList, [rows["USERNAME"], req.session.username], async function(err, sessionData) { if (err) { console.log(err); } else { numOfSessionsLeft.numberOfSessionsLeftIs = 1; console.log(numOfSessionsLeft.numberOfSessionsLeftIs) } }) }) resolve() })
}
for(var i=0;i<rows.length;i++({等待trainerClientList((rows[i][quot;NUM_OF_SESSIONS_PER_MONTH"]=(parseInt(rows[i][quot;NUM_OF_SESIONS_PER_MONTH"(-numOfSessionsLeft.numberOfSessions左撇子(console.log(numOfSessionsLeft(}