为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复 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 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信