Help

Re: Append linked record using API?

Solved
Jump to Solution
1549 1
cancel
Showing results for 
Search instead for 
Did you mean: 
zoul
4 - Data Explorer
4 - Data Explorer

Hello! I have two linked tables, let’s say Users and Events, and the Events table contains an Attendees column that links to Users. I want to add a new value to this column using the API. The code is quite simple:

await eventTable.update(eventId, { Attendees: [userId] });

But this overwrites the previous attendees. Can I just add one new value to the cell without overwriting the previous ones? I know I can read the previous values manually, add the new one and write the whole array back – but that’s a race condition waiting to happen, isn’t it? Is it possible that someone changes the value after I read the previous contents and before I write the new, augmented value?

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

This is what you have to do.

Yes, it is possible to have a race condition. However, there is no other option.

See Solution in Thread

3 Replies 3
kuovonne
18 - Pluto
18 - Pluto

This is what you have to do.

Yes, it is possible to have a race condition. However, there is no other option.

@zoul - I have no idea if this will get rid of your race condition, but a better way to add to an existing array is to use the spread operator.

From the docs:

It is commonly used when you want to add a new item to a local data store, or display all stored items plus a new addition. A very simple version of this kind of action could look like so:

let numberStore = [0, 1, 2];
let newNumber = 12;
numberStore = [...numberStore, newNumber];

It will not really affect the possibility of a race condition. You are still reading the previous value and adding the new one before writing the whole array back. This will not affect the fact that someone else could have updated the field in between the read and the write. At best, it will slightly decrease the time between the read and the write, but it is impossible to reduce that time to zero.

On the other hand, given how most Airtable bases are used, it is unlikely that this will be a problem in most situations. If this type of split second timing important, Airtable may not be the right platform.