Jan 28, 2022 07:35 AM
I’m working on a rather bulky script, and because I’m trying to keep it clean I try and create functions for repetitive tasks. But it seems that using functions inside the scripting block also results in a poor logging behaviour in the output screen.
Take the following code:
let firstCreatedFieldID = await base.getTable("Test table").createFieldAsync('test', 'singleLineText');
console.log(firstCreatedFieldID);
let secondCreatedFieldID = await base.getTable("Test table").createFieldAsync('test', 'singleLineText');
console.log(firstCreatedFieldID);
It neatly outputs the created field ID of the first command, and outputs the expected error of the second command.
But now, if I functionize this script – which is this example seems silly, but in a larger script it of course makes total sense – like this:
createField();
createField();
async function createField() {
let firstCreatedFieldID = await base.getTable("Test table").createFieldAsync('test', 'singleLineText');
console.log(firstCreatedFieldID);
}
None of it gets logged! Not the created field ID, nor the error. Even though the field has successfully been created.
Very inconvenient.
Does someone know if I’m doing something wrong here? Or any suggestions for a better logging setup?
Solved! Go to Solution.
Jan 28, 2022 08:59 AM
You need to invoke the code after “functionizing” it, as you said:
await createField();
Add that line below the code you screen-grabbed and it should work.
Jan 28, 2022 08:59 AM
You need to invoke the code after “functionizing” it, as you said:
await createField();
Add that line below the code you screen-grabbed and it should work.
Jan 28, 2022 12:44 PM
My god. I’m humbled (and ashamed).
Just adding await
in front of calling the function did it.
Thanks @Dominik_Bosnjak
Jan 29, 2022 06:53 AM
Funny enough, I completely missed those top two lines.
I would strongly suggest against relying on hoisting in your Airtable scripts, at least while you’re still in the early days of getting acquainted with the language. Declare functions, then use them - your code will be more readable, predictable, and maintainable.