亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復 Await 在循環中因異步錯誤而失敗

如何修復 Await 在循環中因異步錯誤而失敗

揚帆大魚 2023-03-24 15:12:20
這段代碼一直有效,直到我將它放入 ForEach 循環中。問題似乎出在“等待獲取(integromat_webhook_url + aj)”;行,因為它拋出此錯誤“await is only valid in async function”。我正在嘗試將多個 webhook 發送到 integromat。有沒有辦法不使用 AWAIT 部分或使其成為 ASYNC 功能?我是一個菜鳥,只是在學習 javascript :-)。謝謝喬納森console.log('Filtered PIDs:', filteredPids);let worksheetsCreated = filteredPids.length;let integromat_webhook_url = "";if(worksheetsCreated > 0){        output.markdown(worksheetsCreated + " worksheets created and being sent to indiviudal groups.");    //ADD FILTERED PRODUCTION WORKSHEETS TO TABLE    let recordsCreated = await batchAnd('Create', groupworksheetsBase, filteredPids);    //GET ARRAY OF GROUPS IN FILTERED PRODUCTION WORKSHEET    let unique = [...new Set(filteredPids.map(item => item.fields.Group))];    console.log('unique groups in filtered PIDs',unique);        //LOOP THROUGH UNIQUE GROUPS       unique.forEach(function(uGroup) {        integromatArray = filteredPids.filter(pid => pid.fields.Group == uGroup)        console.log(uGroup, integromatArray);        switch(uGroup) {          case 'Birkenhead':          integromat_webhook_url = "https://hook.integromat.com/mksobdvdxxxxxxxxxxx?pidsArray=";          break;          case 'Taupo':          integromat_webhook_url = "https://hook.integromat.com/9c6y4279kxxxxxxxxxx?pidsArray=";          break;        }        const aj = JSON.stringify(integromatArray);        console.log('stringify array',aj);        await fetch(integromat_webhook_url + aj);    });  } else {    output.markdown("No new worksheets to add.");}
查看完整描述

3 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

只需在 function 關鍵字之前添加 async


unique.forEach(async function(uGroup) {

        integromatArray = filteredPids.filter(pid => pid.fields.Group == uGroup)

        console.log(uGroup, integromatArray);

        switch(uGroup) {

          case 'Birkenhead':

          integromat_webhook_url = "https://hook.integromat.com/mksobdvdxxxxxxxxxxx?pidsArray=";

          break;

          case 'Taupo':

          integromat_webhook_url = "https://hook.integromat.com/9c6y4279kxxxxxxxxxx?pidsArray=";

          break;

        }

        const aj = JSON.stringify(integromatArray);

        console.log('stringify array',aj);

        await fetch(integromat_webhook_url + aj);


    }); 


查看完整回答
反對 回復 2023-03-24
?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

我用這種方法讓它按照我的一個朋友的建議工作......


console.log('Filtered PIDs:', filteredPids);


let worksheetsCreated = filteredPids.length;

let integromat_webhook_url = "";


if(worksheetsCreated > 0){

    

    output.markdown(worksheetsCreated + " worksheets created and being sent to indiviudal groups.");


    //ADD FILTERED PRODUCTION WORKSHEETS TO TABLE

    let recordsCreated = await batchAnd('Create', groupworksheetsBase, filteredPids);


//GET ARRAY OF GROUPS IN FILTERED PRODUCTION WORKSHEET

    const unique = [...new Set(filteredPids.map(item => item.fields.Group))];

    console.log('unique groups in filtered PIDs', unique);


    //LOOP THROUGH UNIQUE GROUPS

    await Promise.all(

        unique.map(async uGroup => {

            integromatArray = recordsArray.filter(pid => pid.fields.Group == uGroup);

            console.log(uGroup, integromatArray);

            switch (uGroup) {

                case 'Birkenhead':

                    integromat_webhook_url = 'https://hook.integromat.com/mksobdvdu8uydiq9x22mxptgaye6ueji?pidsArray=';

                    break;

                case 'Taupo':

                    integromat_webhook_url = 'https://hook.integromat.com/9c6y4279kydmqm7hswjutwhp7fu814aa?pidsArray=';

                    break;

            }

            const aj = JSON.stringify(integromatArray);

            console.log('stringify array', aj);

            console.log('url',integromat_webhook_url + aj);

            const result = await fetch(integromat_webhook_url + aj);

            return result;

        })

    );


 

} else {

    output.markdown("No new worksheets to add.");

}


查看完整回答
反對 回復 2023-03-24
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

將 forEach 循環更改為 for 循環,并且不要忘記將 async 關鍵字放在 function 關鍵字之前,例如:

async function test() {
}


查看完整回答
反對 回復 2023-03-24
  • 3 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號