Help

Async/Await function to loop through and POST to external URL - Works when running externally in node but cannot get it to work in Airtable scripts app

Topic Labels: Scripting extentions
Solved
Jump to Solution
3902 5
cancel
Showing results for 
Search instead for 
Did you mean: 
Vic_Graves
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello! I have written a script which should be taking Airtable data (the actual use-case is URL redirects data) and posts to Shopify, however, I’m getting really stuck with this final element of my script!

I’m using the scripting app in Airtable. I’m fairly confident that this is an Airtable scripting app nuance because when I run this script in node with hardcoded Airtable data it all works as expected and posts my data to Shopify!

In the script below, I’m able to get the Airtable data as expected, and I’m also able to POST to Shopify outside of this function. However, I need this URL inside of a loop (forEach) so that I can POST each of the individual data sets to Shopify.

For reference, URLSettings() updates the body element of the JSON I am posting to Shopify. I am needing to loop in each item of the array and post one-by-one.

For reference, the console.log(result) below gives me an empty array.

const hardcodeArray = [
    '{"redirect":{"path":"/products/orange","target":"/collections/citrus"}}', 
    '{"redirect":{"path":"/products/lemon","target":"/collections/citrus"}}', 
    '{"redirect":{"path":"/products/lime","target":"/collections/citrus"}}'
    ]

async function push() {
    let result = [];
    let arr = hardcodeArray;
    console.log(arr.length);
    arr.forEach(async (element) => {
        let data = await remoteFetchAsync(shopifyURL, URLSettings(element)); 
        let postData = await data.json();
        result.push(postData)
        }
    )
    console.log(result)
}
push();

Hopefully this is the right place to post, but please do let me know if it should be somewhere else?!

Thanks in advance!!

1 Solution

Accepted Solutions

Glad to hear that you solved it! This is just a guess, but it might have failed because you weren’t calling that wrapping async function with the await keyword, so it might not have actually been running asynchronously. You might try putting it back and calling it with await push() instead of just push() and see if it works correctly.

See Solution in Thread

5 Replies 5

Welcome to the community, @Vic_Graves! :grinning_face_with_big_eyes: This is the right place. :thumbs_up:

The .forEach() array method doesn’t work well with asynchronous functions. You’re giving the .forEach() method an asynchronous function to call, but it’s not actually being called asynchronously each time, which explains why you have an empty result array at the end. I suggest reformatting that part of the code using a standard for...of loop:

...
    for (let element of arr) {
        let data = await remoteFetchAsync(shopifyURL, URLSettings(element)); 
        let postData = await data.json();
        result.push(postData)
    }
...

Hi @Justin_Barrett thank you so much for responding and looking into this!! Still a beginner so really appreciate the help!!

I’ve tried updating with your code, and I’m still getting no response, which is confusing! Attached a screenshot of my actual code - you can see from my console.logs that the array is there before the for loop, but then line 100 logs only one element, which makes me think that the loop stops at line 101?

Screenshot 2021-10-29 at 09.27.06

Vic_Graves
5 - Automation Enthusiast
5 - Automation Enthusiast

Ok, I’ve actually solved it! All I did was remove the wrapping async function and it works!!!

Glad to hear that you solved it! This is just a guess, but it might have failed because you weren’t calling that wrapping async function with the await keyword, so it might not have actually been running asynchronously. You might try putting it back and calling it with await push() instead of just push() and see if it works correctly.

Vic_Graves
5 - Automation Enthusiast
5 - Automation Enthusiast

Ahha!!! You are totally right!! Thanks @Justin_Barrett that actually worked also!!!