Skip to main content

Hi Dear community

 

i’m facing a scipting error 

pleaase help me

i need to connect  my ingredients tables to the API open food facts

Thanks

 

 

 

let tableIngredients = base.getTable("Ingrédients");
let tableOFF = base.getTable("Open Food Facts");

let ingredients = await tableIngredients.selectRecordsAsync();
let offRecords = await tableOFF.selectRecordsAsync();

for (let record of ingredients.records) {
    let ingredientName = record.getCellValue("Nom ingrédient");
    if (!ingredientName) continue;

    let response = await fetch(https://world.openfoodfacts.org/cgi/search.pl?search_terms=${encodeURIComponent(ingredientName)}&search_simple=1&action=process&json=1);
    let data = await response.json();

    let product = data.products && data.products.length > 0 ? data.products[0] : null;
    if (!product) continue;

    let nutriments = product.nutriments;
    let energie = nutriments["energy-kcal_100g"] || null;
    let proteines = nutriments["proteins_100g"] ||

Hey ​@Chef Tarik, what is the error you are getting?

Mike, Consultant @ Automatic Nation 


thanks for replying 

 

 


 


Hi,
your script text is not full 

this part opens loop, which should be closed  in normal script

for (let record of ingredients.records) {
let ingredientName = record.getCellValue("Nom ingrédient");
if (!ingredientName) continue;

let response = await fetch(h..
.....
let energie = nutriments["energy-kcal_100g"] || null;
let proteines = nutriments["proteins_100g"] ||

this should be something like . But you can’t ‘guess’ it without having original end part of code. 
 

  …  || null;

let something= somefunction(...)
let ...more lines…..
await tableIngredients.updateRecordAsync(record,{a_list, of values, to_update})
}

Also, hyperlink in await.fetch(http…. ) must be enclosed in backticks (button on the left from ‘1’ under ‘Esc’ on standard keyboard)


Hi 

Thank you for your response 

Can I ask Omni to write for the script ? 


I never tried using it for script. Airtable scripts are very similar and
i’m building my code from small reusable functions, so it’s easier and faster to write code than explain to AI what should be done in “human language”
I know that you can try to ask ChatGPT, paste part of script, tell that it’s a part of script for Airtable extension, and provide additional info - what this script should do. 
Try both options (Omni and ChatGPT) - the result will depend on how good you describe what script should do, especially column names not mentioned in a script, and the logic of its work.

I would recommend to do a snapshot before running the edited script, because if you don’t understand what the code will do, there is a little chance it can accidentally touch existing date if update command written incorrectly.


Well, i tried,
(actually I did a screenshot for other topic, but maybe you can can find something useful)
 



and it was 95% ready to work script,

 


it took two minutes to fix it. That version worked OK even with some redundant parts, like “Change some STATUS, a variable not displayed anywhere”

 


Finally, I did some changes to make it usable for any kind of text, removed and compacted some things. It can be used ‘as is’.
 


Regarding your script, i think it can do the final part.
Change your last line     let proteines = nutriments["proteins_100g"] ||
to

  console.log({products, nutrients})
}


If your Ingrédients table is not too big. It will print data fetched from API. 
Then you need to explain to AI how to use it and what is the role of second opened table,
‘Open Food Facts’
By the way, sometimes I asked ChatGPT to write prompt for Airtable AI and that was helpful.