Feb 13, 2023 01:13 AM
My code is really long so I'm not posting the whole thing, but here's the gist:
let choice = await input.buttonsAsync('Is the stock received from a pending order, transfer or unsure?', ['Pending Order Received', 'Transfer', 'Unsure']);
if (choice == 'Pending Order Received')
{
//code here with no errors
}
else if (choice == 'Transfer')
{
//code here with no errors
}
else if (choice == 'Unsure')
{
//code here
}
I'm getting this error with the last if statement for 'Unsure' and I don't know how to fix it:
Solved! Go to Solution.
Feb 13, 2023 10:10 AM
I think you missed a { between each 'else' and 'if', and the corresponding }} to close them at the end.
Did I hit it?
Feb 13, 2023 02:38 AM
Weird, there's no error when I paste your code into a script extension
Feb 13, 2023 10:10 AM
I think you missed a { between each 'else' and 'if', and the corresponding }} to close them at the end.
Did I hit it?
Feb 13, 2023 11:47 AM
Hey @Sarah_Viljoen!
Huh... no idea what's causing intellisense to flag that.
Similar to @TheTimeSavingCo, I pasted in your script and intellisense didn't flag it for me. Nevertheless, just because intellisense flags a potential issue doesn't mean that your script will actually fail to run or throw an exception.
You can test it by simply tossing in a console.log() into your code block for the else...if condition in question and confirm that it actually runs as intended.
There are many things that you'll commonly do when scripting in Airtable that intellisense isn't a fan of.
In any other IDE, we'd be able to just flag that it should be ignored. Unfortunately, Airtable's built-in code editor doesn't allow us to do that.
Feb 13, 2023 10:44 PM
Yes !! I have a lot of embedded if statements in this code and went over it 3 times before posting but I guess the fourth check was needed. Thank you 😊!
Feb 14, 2023 07:00 AM
I feel like leaving a tiny tip for coders that, like you, at times get trapped in a spiderweb of if-else 🙂
Look back at your original code. Not visually bad, but logically imprecise, as the editor pointed out.
Now look at this beauty:
if (choice == 'Pending Order Received') {
//code here
} else {
if (choice == 'Transfer') {
//code here
} else {
if (choice == 'Unsure') {
//code here
}}}
The key to all this is the little cookie: } else {
Most importantly, it helps you preventing the omition of a bracket. The error message when you do will be clear.
Added benefit is visual: It allows to realign all ‘if’s according to their logical value. There should not be any indentations among the three choices. All them have the same logical hierarchy, none subordinates to other.
So you may want to keep at hand this little cookie: } else {
and use it at every opportunity, to make your code neater and more compact.
Feb 14, 2023 10:08 PM
@Grunty Thanks for this 😊