Help

Re: SyntaxError: await is only valid in async function

1785 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Andrew_Davies
7 - App Architect
7 - App Architect

Hi

I am rather new to coding and was rather proud of myself to get the following script working. I just wanted to list all the field names, types and descriptions, for each table in my base, in another table called “Field list”.

It worked on my test base, but when I run in on the base I wanted to use it with, it only writes approx 14 records. Oddly in the console, it lists all the fields and descriptions.

I tried using await - but get the above error (I think this is something to do with trying to write too many records in one go? I am sure my script is bad and also suspect this is a simple error. Any advice appreciated.

Script below

Thanks,
Andrew

const tables = [“Transactions”, “Suppliers”, “Master Budget”, “Primary budget lines”, “Secondary budget lines”];

tables.forEach(function (table_name) {
console.log('Looping through ',table_name);
let input_table = base.getTable(table_name);

for (let field of input_table.fields) {
    let name = field.name
    let description = field.description
    let field_type = field.type
    console.log(`Field "${field.name}" has description "${field.description}" in table "${table_name}".`);

    await output_table.createRecordsAsync([
        
    {
      fields: {
            'Field name': name,
            'Field description': description,
            'Table name': table_name,
            'Field type': field_type,
        },
    },

])

}

});
output.text(‘Done!’);

2 Replies 2

You have an anonymous function in your forEach loop, which is why the editor is complaining about not using await without declaring the function as async. The easiest fix is to convert that forEach loop into a regular for loop, like the loop you have for your fields in the table. Another fix is to add the async keyword in front of the anonymous function, but depending on the number of tables, you might run into other issues.

You also were probably having 15 records written, rather than 14. Scripting has a rate limit of 15 create record requests per second, and without the await keyword, you exceed this limit.

If you want to learn more coding, I also recommend learning how to create records in batches, which is much more efficient than creating them one at a time. If you look at the Find and Replace example script, you can see an example for updating records in batches. Creating records in batches works in a similar manner.

Finally, I suggest checking out the Field List app. The free version will make a table like what you want, except for the field description. If you want the field description, you will need to purchase a premium license, but that premium license also gets a lot more info, such as what fields are used to compute formula fields, and field options for select fields.

This is so helpful - thank you!

I have a lot to learn, so think I need to look at the basics a little more first. Am working through the Find & replace script now.

Thanks again
Andrew