Help

Logging/debugging does not work using functions – suggestions?

Topic Labels: Scripting extentions
Solved
Jump to Solution
1469 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Tom_Keysers
6 - Interface Innovator
6 - Interface Innovator

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.
Screenshot 2022-01-28 at 16.20.25

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.
Screenshot 2022-01-28 at 16.21.36

Very inconvenient.

Does someone know if I’m doing something wrong here? Or any suggestions for a better logging setup?

1 Solution

Accepted Solutions
Dominik_Bosnjak
10 - Mercury
10 - Mercury

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.

See Solution in Thread

3 Replies 3
Dominik_Bosnjak
10 - Mercury
10 - Mercury

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.

Tom_Keysers
6 - Interface Innovator
6 - Interface Innovator

My god. I’m humbled (and ashamed).
Just adding await in front of calling the function did it.

Thanks @Dominik_Bosnjak

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.