Accessing Result from Node (particularly IF node)

,

This is not a problem/bug, but more a functionality question (functionality for this might not exist). How can one access the result of an IF node within a code node? E.g. below I want to know in the Code node what the outcome of the IF node was (I’ve set to true for testing):

Hey @pb84,

Normally you wouldn’t use an If and a Code node like that as it would result in the code node running twice once for each output so you would generally know in your node if it was true or false based on where in your flow it was.

…but to be complete and actually answer the question you have, You can use $prevNode.outputIndex which will tell you which output index the previous node is sending the data from where 0 is true and 1 is false so your code node would be something like…

let if_output = $prevNode.outputIndex ? false : true; // This should be what the result of the if statement is, in this case expecting true

return [{
  json:{
    "if_output": if_output
  }
}]
2 Likes

Perfect Jon this is great - long story short I need the if node to do some stuff conditionally but the paths then merge

2 Likes

Does this have to be $prevNode? Can I do this with a node thats further back by specifying the node name?

Hey @pb84,

It only works with the previous node, If you wanted to get the if node result later on to work out if it was true or false you could maybe use a code node with $('node-name').all(0) for all true branch results and $('node-name').all(1) for all false branch results.

1 Like

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