Mar 25, 2022 04:53 PM
I have the following code that runs fine if I put it in the main body of my App but not if I put it in a function. I’m baffled, hoping folks here can enlighten me as to why. When in the main body, it runs through all of the records in the Teams table. When in a function, only the 1st record is updated. The portion I’d like to have in a function is the code inside of the if (allUsers) block. I have other functions that update in loops, what is so special about this one?
let teamsTable = base.getTable("Teams");
let teamsRecords = await teamsTable.selectRecordsAsync({
sorts: [{field:"active members", direction: 'asc'}],
fields: ["active members", "Included Players"]
});
if (allUsers) {
console.log("rebalancing");
let lowestActiveCount = teamsRecords.records[0].getCellValue("active members");
console.log("Lowest active count: " + lowestActiveCount);
let included = lowestActiveCount - 5;
for (let team of teamsRecords.records) {
console.log("Changing Included to: " + included);
await teamsTable.updateRecordAsync(team.id, {
"Included Players": included
})
}
}
Solved! Go to Solution.
Mar 25, 2022 06:24 PM
Welcome to the Airtable community!
When you put it in a function, make sure that (1) you declare the function as an async
function, and (2) you use the await
keyword when you call the function.
Mar 25, 2022 06:24 PM
Welcome to the Airtable community!
When you put it in a function, make sure that (1) you declare the function as an async
function, and (2) you use the await
keyword when you call the function.
Mar 25, 2022 06:27 PM
Ahhhh I’d missed the “await” on the function call. Thank you!