簡體   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