If expression is undefined

Describe the problem/error/question

I have a node where the data set may be undefined. When this happens I need to have if statment within the expression to echo out something else. Not sure what I’m doing wrong. I’m sure its something simple.

{{ $if($json.data[9].litify_pm__Answer__c === "undefined","long strring x dont findanthing43234230432234", "test")}}

ive also tried

{{ $if($json.data[9].litify_pm__Answer__c === undefined,"long strring x dont findanthing43234230432234", "test")}}

What is the error message (if any)?

Information on your n8n setup

  • **n8n version:**1.5.1
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • Operating system:

if you need flow example the following json is what is built with the merge.

Hi @Philip_Wiggins :wave: Would you happen to have another example of what you’re seeing, or perhaps a more direct one? When I’m running this, I’m seeing this:

Which might be what you’re expecting? :thinking:

@EmeraldHerald The problem occurs when its actually undefined. So in the sample data I gave there are 49 objects. The Google Docs node I have it to set 10 data points for each batch and it errors when it tries to fill in the data for the 50th object. I’m doing this to update in batches of 10, but my data set could be any number of “questions and answer” sets so I expect sometimes to receive undefined. In the sample I gave you if you made it only 9 objects you should get the error. Below you can see if its not undefined the conditional works, but if its actually undefined, it doesn’t.

I’ve also updated the JSON link to have 9 objects instead of 49 so it doesn’t actually have to go through the batches to see the issue.

Can you not check for undefined within the expression? Below is another example where you can see my issue using the $if condtional. I now that data[9] undefined here and when its undefined I want to be able to output something not undefined.

Think I found a solution that will work for me to take care of these using the code node.

1 Like

Ah, nice! Thanks so much for sharing your solution :bowing_man:

Hey @Philip_Wiggins,

You might also be able to use something like {{ $if( $json.data[9]?.question === undefined, "1", "2" ) }} which seems to work in my testing for my test index 8 is the only one that contains any data.

1 Like

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