Skip to main content
Solved

Scripting button & if statement error

  • February 13, 2023
  • 6 replies
  • 26 views

Forum|alt.badge.img+5

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:

Best answer by Grunty

I think you missed a { between each 'else' and 'if', and the corresponding }} to close them at the end.

Did I hit it?

6 replies

TheTimeSavingCo
Forum|alt.badge.img+31
  • Brainy
  • 6460 replies
  • February 13, 2023

Weird, there's no error when I paste your code into a script extension


Grunty
Forum|alt.badge.img+15
  • Inspiring
  • 69 replies
  • Answer
  • February 13, 2023

I think you missed a { between each 'else' and 'if', and the corresponding }} to close them at the end.

Did I hit it?


Ben_Young1
Forum|alt.badge.img+22
  • Brainy
  • 520 replies
  • February 13, 2023

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.


Forum|alt.badge.img+5
  • Author
  • Known Participant
  • 10 replies
  • February 14, 2023

I think you missed a { between each 'else' and 'if', and the corresponding }} to close them at the end.

Did I hit it?


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 😊!


Grunty
Forum|alt.badge.img+15
  • Inspiring
  • 69 replies
  • February 14, 2023

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 😊!


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.


Forum|alt.badge.img+5
  • Author
  • Known Participant
  • 10 replies
  • February 15, 2023

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.


@Grunty  Thanks for this  😊