I have this function, that queries my database, and then calls getNumOfSessionForTrainerClientList
function:
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();
})
})
and then inside the function getNumOfSessionForTrainerClientList
, I have this:
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)
}
})
})
}
however, what is happening is that this line : rows[i]["NUM_OF_SESSIONS_PER_MONTH"] = (parseInt(rows[i]["NUM_OF_SESSIONS_PER_MONTH"]) - numOfSessionsLeft.numberOfSessionsLeftIs)
is actually assigning 0
to rows[i]["NUM_OF_SESSIONS_PER_MONTH"]
because that variable assignment is happening before the function call for getNumOfSessionForTrainerClientList
finishes. So it is happening out of sync. I am not sure how to fix this, i have run into issues with async
await
before, but nothing like this. would appreciate the help.