问题描述我需要做一个返回数据集,是个对象数组awardList,里面的每一个对象有className属性和stuInfo属性,stuInfo属性是一个对象数组,然后里面的对象的属性有name和awardInfo,这些对象和数组都是构造出来的,利用数据库查询出来的数据逐一进行填充,我自己写的代码执行完之后,发现awarsList依旧是一个空数组,感觉还是异步的问题,却找不出问题在哪。问题出现的环境背景及自己尝试过哪些方法之前使用的是回调函数,后来改写成Promise相关代码//请把代码文本粘贴到下方(请勿用图片代替代码)letgetClassInfo=function(jobId){letsql=`SELECTcounselorclasscontact.classId,class.classNameFROMcounselorclasscontact,classWHEREclass.classId=counselorclasscontact.classIdANDcounselorclasscontact.jobId=?`;returnnewPromise(function(resolve,reject){db.query(sql,[jobId],function(results,field){try{resolve(results)}catch(err){reject(err)}})})}letgetStuInfo=function(classId){letsql=`SELECTDISTINCTawardinformation.studentId,student.nameFROMstudent,awardinformationWHEREawardinformation.classId=?ANDawardinformation.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=`SELECTawardinformation.awardName,awardinformation.awardTime,awardinformation.awardAgencyFROMawardinformationWHEREawardinformation.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里面有内容,而且嵌套的对象里面也有内容,
守候你守候我
慕丝7291255
相关分类