Script runs as an app, but not as Automation

I’m trying to update a base every 24 hours hitting a public API using remoteFetchAsync().

When I run the automation code below, I get this error:

ERROR

ReferenceError: remoteFetchAsync is not defined
at on line 24
at main on line 9

The automation:

//output.markdown ("# Updating BTC price");

let table = base.getTable ("Fetch"),
    records = await table.selectRecordsAsync ();

//Check difficulty
await Promise.all (

    records.records.map (async record => {

        let stock = record.getCellValue ("Ticker");

        if (! stock) {

            //output.text ("Can't update the price without Ticker");

            return;
        }

        //output.text (`Updating Mining Difficulty of ${stock}...`);

        // make request to BTC.com
        
        var response = await remoteFetchAsync (
            `https://chain.api.btc.com/v3/block/latest`
        ).then (r => r.json ());

        // Parse by hand

        var data= response.data;
        var diff= data.difficulty; 
        
        // print fetched data
        //output.markdown (`${stock}'s Difficulty right now is: **${diff}**`)

        // update record in table
        return await table.updateRecordAsync (record, {
            "Difficulty": diff         
        });

       
    })
       
);

//Check Price
await Promise.all (

    records.records.map(async record => {

        let coin = record.getCellValue("coin");

        if (!coin) {

            //output.text("Can't update without the name of coin...");

            return;
        }

        output.text(`Updating price of ${coin}`);

        // make request
        
        var requestResult = await remoteFetchAsync(
            `https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=usd`).then(r => r.json());
        // print fetched data
        const price = ((requestResult[coin] || {}).usd);
        output.markdown(`${coin}'s price right now is: **$${price}**`)

        // update record in table
        return await table.updateRecordAsync(record, {
            "Price": price,
        });
    })    
);
 

       
//output.markdown ("# Done!");

As a Script (that I have to run manually), it runs exactly as above. The only difference between the two codes is that output.markdown() is not commented.

What am I missing?

There are several obscure differences between scripting app and automation action scripts. This is one of them.

Replace remoteFetchAsync with a simple fetch. The remoteFetchAsync was introduced in scripting app to deal with cors errors, but automation scripts run on Airtable servers which does not need this workaround, thus remoteFetchAsync does not exist in automation action scripts.

2 Likes

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.