problem description
I need to make a return dataset, which is an object array awardList, in which each object has className property and stuInfo property, stuInfo property is an object array, and then the object properties in it are name and awardInfo. These objects and arrays are constructed. I fill them one by one with the data queried from the database. After the code I wrote is executed, I find that awarsList is still an empty array. It still feels like an asynchronous problem, but I can"t find out what the problem is.
the environmental background of the problems and what methods you have tried
was previously used as a callback function, but was later rewritten as Promise
related codes
/ / Please paste the code text below (do not replace the code with pictures)
let getClassInfo=function (jobId) {
let sql=`SELECT
counselorclasscontact.classId,
class.className
FROM
counselorclasscontact,
class
WHERE
class.classId=counselorclasscontact.classId
AND
counselorclasscontact.jobId = ?`;
return new Promise(function(resolve,reject){
db.query(sql,[jobId],function (results,field) {
try {
resolve(results)
}catch (err) {
reject(err)
}
})
})
}
let getStuInfo=function (classId) {
let sql=`SELECT
DISTINCT awardinformation.studentId,
student.name
FROM
student,
awardinformation
WHERE
awardinformation.classId=?
AND
awardinformation.studentId=student.studentId`;
return new Promise(function(resolve,reject){
db.query(sql,[classId],function (results,field) {
try {
resolve(results)
}catch (err) {
reject(err)
}
})
})
}
let getStuAwardInfo=function (studentId) {
let sql=`SELECT
awardinformation.awardName,
awardinformation.awardTime,
awardinformation.awardAgency
FROM
awardinformation
WHERE
awardinformation.studentId=?`;
return new Promise(function (resolve,reject) {
db.query(sql,[studentId],function (results,field) {
try {
resolve(results)
}catch (err) {
reject(err)
}
})
})
}
exports.getAwardByCounselor = function (send,jobId) {
let awardInfoList=[];
//let classResult=await getClassInfo(jobId);
getClassInfo(jobId).then(function (classResult) {
for(let i=0;i<classResult.length;iPP){
getStuInfo(classResult[i].classId).then(function (stuResult) {
let awardInfoByClass={
className:"",
stuInfo:[]
};
awardInfoByClass.className=classResult[i].className;
if(stuResult.length!==0) {
for (let i = 0; i < stuResult.length; iPP) {
let singleStuInfo = {
stuName: "",
awardInfo: []
}
singleStuInfo["stuName"] = stuResult[i].name;
getStuAwardInfo(stuResult[i].studentId).then(function (singleAwardInfo) {
for (let i = 0; i < singleAwardInfo.length; iPP)
singleStuInfo["awardInfo"].push(singleAwardInfo[i]);
awardInfoByClass["stuInfo"].push(singleStuInfo);
//console.log(awardInfoByClass);
//send(awardInfoByClass);
}, function (error) {
console.log(error);
let results = "error"
send(results)
})
//console.log(awardInfoByClass);
}
}
//console.log(awardInfoByClassz);
awardInfoList.push(awardInfoByClass);
},function (error) {
console.log(error);
let results="error"
send(results)
})
}
console.log(awardInfoList);
send(awardInfoList);
},function (error) {
console.log(error);
let results="error"
send(results)
})
}
what result do you expect? What is the error message actually seen?
the expected result is that there is content in awardList, and there is also content in nested objects.