service.js文件中包含以下代码。
exports.getSeminarDetailsById = async function (seminarId) {
try {
let SeminarList = [];
var seminarData = await SeminarRepository.getSeminarDetailsById(seminarId);
if (seminarData && seminarData.length > 0) {
let userIdList = [...new Set(seminarData.map(x => x.user_id))];
if (userIdList && userIdList.length > 0) {
let userDetails = await EmployeeRepository.getEmployeeDetailsByUserIds(userIdList);
if (userDetails && userDetails.length > 0) {
seminarData.forEach(element => {
let seminarDetail;
let userName = userDetails.filter(x => x.user_id == element.user_id).map(x => x.userfullname)[0];
let categoryName;
if (element.category_id == 1)
categoryName = AppConstants.seminarCategoryName.TECHNICAL;
else
categoryName = AppConstants.seminarCategoryName.NONTECHNICAL;
seminarDetail = new SeminarTrackerDetails(element, userName, categoryName);
await mapAttachmentWithSeminar(seminarId, seminarDetail);
console.log("second", seminarDetail);
SeminarList.push(seminarDetail);
});
}
}
}
return SeminarList;
} catch (err) {
console.log(err);
throw err;
}
}
await mapAttachmentWithSeminar(seminarId, seminarDetail);
出现错误,并且在以下相同文件中定义了该错误。
async function mapAttachmentWithSeminar(seminarId, seminarDetail) {
var seminarAttachmentDetails = await SeminarRepository.getSeminarAttachmentDetailsById(seminarId);
if (seminarAttachmentDetails && seminarAttachmentDetails.length > 0) {
let AttachmentDetails = [];
seminarAttachmentDetails.forEach(element => {
let attachmentDetails = new SeminarAttachmentDetails(element);
AttachmentDetails.push(attachmentDetails);
});
seminarDetail.SeminarAttachmentDetails = AttachmentDetails;
}
else {
seminarDetail.SeminarAttachmentDetails = null;
console.log("first", seminarDetail);
}
}
如果我删除了等待功能,那么在执行功能console.log("second", seminarDetail);
之前,将首先执行mapAttachmentWithSeminar()
。因此,该函数返回的SeminarAttachmentDetails
的值将被遗漏,如下所示:
这是预期的输出。
答案 0 :(得分:1)
您可以使用经典的.forEach
循环来代替for
for(let i = 0; i < seminarData.length; i++){
let element = seminarData[i];
let seminarDetail;
let userName = userDetails.filter(x => x.user_id == element.user_id).map(x => x.userfullname)[0];
let categoryName;
if (element.category_id == 1)
categoryName = AppConstants.seminarCategoryName.TECHNICAL;
else
categoryName = AppConstants.seminarCategoryName.NONTECHNICAL;
seminarDetail = new SeminarTrackerDetails(element, userName, categoryName);
await mapAttachmentWithSeminar(seminarId, seminarDetail);
console.log("second", seminarDetail);
SeminarList.push(seminarDetail);
}
答案 1 :(得分:0)
每个嵌套函数以及父函数都应声明为async
。
如果您在嵌套级别之一中缺少此声明,请注意这部分代码
seminarData.forEach(async element => {
// ^^^^ async missed here
let seminarDetail;
let userName = userDetails.filter(x => x.user_id == element.user_id).map(x => x.userfullname)[0];
let categoryName;
if (element.category_id == 1)
categoryName = AppConstants.seminarCategoryName.TECHNICAL;
else
categoryName = AppConstants.seminarCategoryName.NONTECHNICAL;
seminarDetail = new SeminarTrackerDetails(element, userName, categoryName);
await mapAttachmentWithSeminar(seminarId, seminarDetail);
console.log("second", seminarDetail);
SeminarList.push(seminarDetail);
});
答案 2 :(得分:0)
首先,您使用了错误的异步等待。您正在等待非异步功能的范围。 在这里:
seminarData.forEach(element => {
let seminarDetail;
let userName = userDetails.filter(x => x.user_id == element.user_id).map(x => x.userfullname)[0];
let categoryName;
if (element.category_id == 1)
categoryName = AppConstants.seminarCategoryName.TECHNICAL;
else
categoryName = AppConstants.seminarCategoryName.NONTECHNICAL;
seminarDetail = new SeminarTrackerDetails(element, userName, categoryName);
await mapAttachmentWithSeminar(seminarId, seminarDetail);
console.log("second", seminarDetail);
SeminarList.push(seminarDetail);
});
你应该有这个
// here you have the SeminarList but promisified
// with the form Promise<SeminarListItem>[]
// to get the values out of the promises you have to await this array
const promisifiedSeminarList = seminarData.map((element)=>{
let userName = userDetails.filter(x => x.user_id == element.user_id).map(
x => x.userfullname)[0]
);
const categoryName = elemenet.category_id == 1
? AppConstants.seminarCategoryName.TECHNICAL
: AppConstants.seminarCategoryName.NONTTECHNICAL;
const seminarDetail = new SeminarTrackerDetails(element, userName, categoryName);
return mapAttachmentWithSeminar(seminarId, seminarDetail);
});
// now
const seminarList = await Promise.all(promisifiedSeminarList);
为此,您需要mapAttachmentWithSemniar函数返回一个未发生的值