How do I access nested data from a IF node?

,

Hey n8n community,

Can anyone explain why this IF node data is not accessible when I use a $ifEmpty() expression?

The node returns undefined even though there is data and if I access without the $ifEmpty() expression the data is read without issue.

Steps

  • I’m checking if the user exists in my DB then using the IF node to funnel based on the outcome.
  • If TRUE, the output is an empty array, therefore I thought the $ifEmpty() would be perfect for this as if FALSE the node branches to a create user node.
  • At the stage where I’m getting the error there will always be data returned either from the TRUE OR FALSE outcome.
  • I’ve even tried just declaring $json.id since there will always be an id but this also causes undefined to be returned.
  • I’ve also tried accessing the node directly by name

Flow

Error

Information on your n8n setup

  • n8n version: 1.80.5
  • Database:SQLite
  • n8n EXECUTIONS_PROCESS setting:own, main
  • Running n8n via :Docker
  • Operating system: Linux Ubuntu 20.04

Please try the following:

Disconnect the other IF branch, and leave only the one you are testing.

Sometimes multi-branch testing gets messy, even though it works fine when running in execution mode.

Try to get the $json.id and $json.items[0].id without the $ifEmpty function.

If they show up, you are correct and this is a bug. But if one of them shows up as undefined, then you should change your logic.

You could try these, instead of ifEmpty:

  • {{ $if(typeof $json.id == 'undefined', $json.items[0].id) }}
  • {{ $json.id || $json.items[0].id }}
  • {{ $json.id ?? $json.items[0].id }}

Let us now what option works best for you!

:point_right: If my reply answers your question, please remember to mark it as a solution.

1 Like

Thanks @solomon I did try without the $ifEmpty and the data was returned but only in test mode. With live data it did not.

I ended up using {{ $json.id ?? $json.items[0].id }} which works.

1 Like