繁体   English   中英

Node Js - SyntaxError: await 仅在异步中有效 function

[英]Node Js - SyntaxError: await is only valid in async function

我在 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);
  }
}

如果我删除等待 function,然后console.log("second", seminarDetail); 将在执行 function mapAttachmentWithSeminar()之前首先执行。 这样从SeminarAttachmentDetails返回的 SeminarAttachmentDetails 的值将被遗漏,如下所示:

在此处输入图像描述

这是预期的 output。

在此处输入图像描述

而不是使用.forEach你可以 go 与经典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);
}

每个嵌套的 function 都应声明为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);
});

首先,您使用了错误的异步等待。 您正在 function 的 scope 内部等待,它不是异步的。 这里:

 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);

为此,您需要 function mapAttachmentWithSemniar 返回一个没有发生的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM