简体   繁体   English

如何修复 Await 在循环中失败并出现异步错误

[英]How to fix Await fails in loop with Async error

This code worked until I put it in a ForEach loop.这段代码一直有效,直到我将其放入 ForEach 循环中。 The issue appears to be with the 'await fetch(integromat_webhook_url + aj);'问题似乎与'await fetch(integromat_webhook_url + aj);'有关line as it throws this error 'await is only valid in async function'.行,因为它抛出此错误“等待仅在异步函数中有效”。

I'm trying to send multiple webhooks to integromat.我正在尝试将多个 webhooks 发送到 integromat。

Is there a way to do without the AWAIT part or make it an ASYNC Function please?有没有办法不用 AWAIT 部分或使它成为 ASYNC 功能?

I'm a noob and just learning javascript :-).我是一个菜鸟,只是在学习 javascript :-)。

Thanks谢谢

Jonathan乔纳森

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.");
}

Thanks so much非常感谢

Jonathan乔纳森

just add async before the function keyword只需在 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);

    }); 

I used this approach to get it working as suggested by a friend of mine...我使用这种方法让它按照我的一个朋友的建议工作......

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.");
}

Change your forEach loop to a for loop and do not forget to put the async keyword before the function keyword like :将您的 forEach 循环更改为 for 循环,并且不要忘记将 async 关键字放在 function 关键字之前,例如:

async function test() {
}

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

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