Accessing different/same values in code

I’m struggling with understanding the syntax for handling data returned by the Compare Datasets note in a code node. I want to set a value based on if the field in question comes through the ‘A only’, ‘B only’, ‘Same’ or ‘Different’ path. The idea is that if the value is in the ‘different’ array/object, use the value from the most recently-modified input, and if not, use the same value if that isn’t undefined, and otherwise default to the value from a previous ‘Set’ node.

The current error message: Problem in node ‘Get process name‘
Cannot read properties of undefined (reading ‘process’) [line 4] TypeError

Here are the two relevant nodes. The Compare Datasets node has an Input A and an Input B, both with a field called ‘process.’

and the JavaScript code:

let process;

// Check to see if the process value is in the different branch. If so, compare timestamps and take the process value from the latest
if (typeof $node["Compare Datasets"].json.different.process !== 'undefined') {
  process = $node["Input A"].json.lastModified > $node["Input B"].json.lastModified ? $node["Compare Datasets"].json.different.process.inputA : $node["Compare Datasets"].json.different.process.inputB;
} 
// If not, check to see if the process value is in the same branch. If so, use that value
else if (typeof $node["Compare Datasets"].json.same.process !== 'undefined') {
  process = $node["Compare Datasets"].json.same.process;
} 
// If not, use the value from Input B (MySQL)
else { 
  process = $node["Input B"].json.process;
}

return process;

Information on your n8n setup

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

Hi @hndmn, you can access the different outputs of a node with multiple outputs by specifying the branchIndex value. So you could do something like this:

This would produce a result telling you which branch a single item is coming from:

Seeing you are going to use the Code node anyway, you could also consider performing the exact comparison you need directly in the Code node and cut out the Compare Datasets node entirely.

2 Likes

OK. I have to think about this. It’s an interesting idea of handling the comparisons via code… I’ll see what we can come up with and post outcomes in case it helps other users.

1 Like

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