Logging/debugging does not work using functions – suggestions?

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?

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.

2 Likes

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.

1 Like

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.