Help

Re: Automated script gives vague error

Solved
Jump to Solution
1263 6
cancel
Showing results for 
Search instead for 
Did you mean: 
Marlene_Kingsto
4 - Data Explorer
4 - Data Explorer

I am attempting to set up an automation and have a script update a linked field in a table. When testing my script the output gives me this error. “Your script has a syntax error” (adding this for search engines cause I could not find this on google at all)

image

The bit of code that makes this error happen is this (it has no syntax highlighted saying it’s an error)

await customersTable.updateRecordAsync(depo, {
    'Dams': dogs
})

depo is from here

let depo = depositors.getRecord(depositors.recordIds[index])

depo looks like this when logged: {id: "rec1231412312", name: "Some name"}

dogs is an array of objects (“dog” records) like this.
image

I’m not sure where to go from here cause this error leads me nowhere. I have tried just passing an array of IDs. I’ve tried passing an array of objects with just the IDs and not the name. Nothing has worked so far. Is anyone able to tell me what I’m doing wrong?

1 Solution

Accepted Solutions

You can use await inside a loop, and inside an anonymous function. You just have to declare the function as an async function.

puppyAvailability.forEach(async (dogs, index) => {

On the other hand, even though this is possible to use await inside the loop, it is better to update the records in bulk after the loop.

See Solution in Thread

12 Replies 12

Hi @Marlene_Kingston, and welcome to the community!

What is the value of depo when the updateRecordAsync() executes?

It appears you are actually trying to update multiple records. If so, you need to review the sample for updateRecordsAsync() -

// Update two records in the Big Tickets table
let table = base.getTable("Big Tickets");
let query = await table.selectRecordsAsync();
let records = query.records;
await table.updateRecordsAsync([
    {
        id: records[0].id,
        fields: {
            "Name": "Update one",
        },
    },
    {
        id: records[1].id,
        fields: {
            "Name": "Update two",
        },
    },
]);

Hi @Marlene_Kingston - if depo is a single record and updateRecord singular is what you are after, then I think it might be dogs array that is at fault here. I’m assuming that ‘Dams’ is a linked field - if so the write format for a linked field is:

Screenshot 2021-02-17 at 08.04.35

So, your dogs array needs to be:

[{id: 'recabc123456789'}, {id: 'recxyz43434343'}]

so exclude the name attribute

‘Dams’ is a linked and I am attempting to update only a single record. I have tried the format you suggested but no dice.

image

It still gives the same error.

depo is a record from the customers table obtained from the line I mentioned above depositors.getRecord(depositors.recordIds[index]) so it looks something like this when I log it:

{id: "rec1231412312", name: "Some name"}

I am attempting to update a linked field in a single record with the dog IDs. I tried the suggestion JonathanBowen gave in his answer, but it doesn’t seem to work. Or maybe that fixed an issue but there is still another.

Okay - if you’re going to do this (above, a single record update), the first issue you need to resolve is that “depo” must be a record ID, not an object as it appears is the case.

If depo is actually an object as you describe, i.e., …

{id: “rec1231412312”, name: “Some name”}

… then you need to specify depo.id to successfully call the Airtable API.

I would test this with dogs hardcoded as “Fido” just to rule out any other issues.

It appears that you can’t update records within a loop. I didn’t give the full context of the code because I didn’t think it was necessary, but apparently it was.

So I added a testing field as a single line text field just to be sure it wasn’t anything with the field type and this still didn’t work.

puppyAvailability.forEach((dogs, index) => {
        let depo = depositors.getRecord(depositors.recordIds[index])

        await customersTable.updateRecordAsync(depo.id, {
             'testing': 'Fido'
        })
    }
})

So I pulled the record update function outside of the loop and hardcoded values with the original fields and it worked just fine. (After I used depo.id). So I’m assuming airtable doesn’t like to update within a loop?

let depo = depositors.getRecord(depositors.recordIds[0])
await customersTable.updateRecordAsync(depo.id, {
    'Dams': puppyAvailability[0]
})

I was hoping to update within the loop, but it looks like I’ll have to restructure the data to work with the multiple record update.

Um, no. We perform iterative updates all the time. In fact, when you rebuild your code, factor in the use of updateRecordsAsync() for fifty at a time unless you want this to take forever. :winking_face:

I think you have a map issue in your code. Something is not mapping properly from puppyAvailability to depositors and index.

I got it figured out. It doesn’t let you await in a loop. Not sure why. I just took out await and it worked.

I’ve heard this many times just before a developer says - hey, why would some records not get updated from this looping process. :winking_face:

Do you know why it works without await but not with it? If it works without await then the data/data structure can’t be the issue. I’m pretty sure it’s an airtable quirk because I’ve done this in other javascript applications.

You can use await inside a loop, and inside an anonymous function. You just have to declare the function as an async function.

puppyAvailability.forEach(async (dogs, index) => {

On the other hand, even though this is possible to use await inside the loop, it is better to update the records in bulk after the loop.

Ok, I can’t believe I forgot to mark my function in my loop as async. You’re right about updating them in bulk, but I have over 50 records that need to update so I still have to update within the loop, but I currently have it set up to update the max allowed records at a time on each iteration. Thank you.

I might also mention that I needed to change the way I looped since the await doesn’t run in a forEach. I changed it to a for of loop and wrapped it in an async function. Now everything is working as expected.