js异步构建包含对象的数据返回集问题十分感谢

问题描述
我需要做一个返回数据集,是个对象数组awardList,里面的每一个对象有className属性和stuInfo属性,stuInfo属性是一个对象数组,然后里面的对象的属性有name和awardInfo,这些对象和数组都是构造出来的,利用数据库查询出来的数据逐一进行填充,我自己写的代码执行完之后,发现awarsList依旧是一个空数组,感觉还是异步的问题,却找不出问题在哪。
问题出现的环境背景及自己尝试过哪些方法
之前使用的是回调函数,后来改写成Promise
相关代码
//请把代码文本粘贴到下方(请勿用图片代替代码)
letgetClassInfo=function(jobId){
letsql=`SELECT
counselorclasscontact.classId,
class.className
FROM
counselorclasscontact,
class
WHERE
class.classId=counselorclasscontact.classId
AND
counselorclasscontact.jobId=?`;
returnnewPromise(function(resolve,reject){
db.query(sql,[jobId],function(results,field){
try{
resolve(results)
}catch(err){
reject(err)
}
})
})
}
letgetStuInfo=function(classId){
letsql=`SELECT
DISTINCTawardinformation.studentId,
student.name
FROM
student,
awardinformation
WHERE
awardinformation.classId=?
AND
awardinformation.studentId=student.studentId`;
returnnewPromise(function(resolve,reject){
db.query(sql,[classId],function(results,field){
try{
resolve(results)
}catch(err){
reject(err)
}
})
})
}letgetStuAwardInfo=function(studentId){
letsql=`SELECT
awardinformation.awardName,
awardinformation.awardTime,
awardinformation.awardAgency
FROM
awardinformation
WHERE
awardinformation.studentId=?`;
returnnewPromise(function(resolve,reject){
db.query(sql,[studentId],function(results,field){
try{
resolve(results)
}catch(err){
reject(err)
}
})
})
}
exports.getAwardByCounselor=function(send,jobId){
letawardInfoList=[];
//letclassResult=awaitgetClassInfo(jobId);
getClassInfo(jobId).then(function(classResult){
for(leti=0;igetStuInfo(classResult[i].classId).then(function(stuResult){
letawardInfoByClass={
className:'',
stuInfo:[]
};
awardInfoByClass.className=classResult[i].className;
if(stuResult.length!==0){
for(leti=0;iletsingleStuInfo={
stuName:'',
awardInfo:[]
}
singleStuInfo['stuName']=stuResult[i].name;
getStuAwardInfo(stuResult[i].studentId).then(function(singleAwardInfo){
for(leti=0;isingleStuInfo['awardInfo'].push(singleAwardInfo[i]);
awardInfoByClass['stuInfo'].push(singleStuInfo);
//console.log(awardInfoByClass);
//send(awardInfoByClass);
},function(error){
console.log(error);
letresults='error'
send(results)
})
//console.log(awardInfoByClass);
}
}
//console.log(awardInfoByClassz);
awardInfoList.push(awardInfoByClass);
},function(error){
console.log(error);
letresults='error'
send(results)
})
}
console.log(awardInfoList);
send(awardInfoList);
},function(error){
console.log(error);
letresults='error'
send(results)
})
}
你期待的结果是什么?实际看到的错误信息又是什么?
期待的结果是awardList里面有内容,而且嵌套的对象里面也有内容,
千万里不及你
浏览 443回答 2
2回答

守候你守候我

已解决!源代码如下:letgetClassInfo=async(jobId)=>{letsql=`SELECTcollege.collegeName,counselorclasscontact.classId,class.classNameFROMcounselorclasscontact,class,collegeWHEREclass.classId=counselorclasscontact.classIdANDclass.collegeId=college.collegeIdANDcounselorclasscontact.jobId=?`;returnnewPromise(async(resolve,reject)=>{try{letclassResult=awaitdb.queryByPromise(sql,jobId);letparseReuslt=JSON.parse(JSON.stringify(classResult));resolve(parseReuslt);}catch(e){reject(e);}})}letgetStuInfo=async(classId)=>{letsql=`SELECTDISTINCTawardinformation.studentId,student.nameFROMstudent,awardinformationWHEREawardinformation.classId=?ANDawardinformation.studentId=student.studentId`;returnnewPromise(async(resolve,reject)=>{try{letstuInfo=awaitdb.queryByPromise(sql,classId);letparseReuslt=JSON.parse(JSON.stringify(stuInfo));resolve(parseReuslt);}catch(e){reject(e);}})}letgetStuAwardInfo=async(studentId)=>{letsql=`SELECTawardinformation.awardName,awardinformation.awardTime,awardinformation.awardAgencyFROMawardinformationWHEREawardinformation.studentId=?`;returnnewPromise(async(resolve,reject)=>{try{letstuAwardList=awaitdb.queryByPromise(sql,studentId);letparseReuslt=JSON.parse(JSON.stringify(stuAwardList));resolve(parseReuslt);}catch(e){reject(e);}})}exports.getAwardByCounselor=async(jobId)=>{returnnewPromise(async(resolve,reject)=>{try{letclassInfo=awaitgetClassInfo(jobId);for(leti=0;i{classInfo[i].stuInfo.push(item);})}if(classInfo[i].stuInfo.length!==0){console.log(classInfo[i].stuInfo);for(letj=0;j

慕丝7291255

错误:returnnewPromise(function(resolve,reject){db.query(sql,[jobId],function(results,field){try{resolve(results)}catch(err){reject(err)}})})首先,回调函数的第一个参数一定是error,也就是说你这里的results并不是你期待的结果,实际是出错情况下的错误信息。其次,try...catch这里没有任何作用,你要捕获的错误应该是执行db.query可能抛出的异常,这个异常对应回调的第一个参数,但是你却捕获resolve。修正:returnnewPromise(function(resolve,reject){db.query(sql,[jobId],function(err,results,field){if(err)reject(err);resolve(results);})})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript