Targeting Previous Switch Node Outputs

Describe the problem/error/question

I’m currently working on building a workflow for automating a Discover Weekly archive playlist on Spotify. I would prefer to use a Switch node as it makes the workflow look cleaner, however, I’m having a difficult time attempting to access the specific output chain containing the found archive playlist further down the workflow. My Switch node is set up with 2 outputs, the first output finding the Discover Weekly playlist by name and the second output finding the archive playlist by name. When attempting to access the archive playlist URI within a later Spotify action node from this Switch, I’m only able to access the first output chain. I can’t seem to find the right expression (if there is one) to target the second output chain to grab the playlist URI. Am I overthinking this and should use the IF or Filter node? Thank you for your time and assistance in advance!

Please share your workflow

Information on your n8n setup

  • n8n version: 1.21.1
  • Database (default: SQLite): SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: DietPi/CasaOS (Zimaboard)

@jyln , here’s the document outlining how to access previous nodes, Output of other nodes | n8n Docs. You need to bear in mind that your Switch has more than 1 output, which implies specifying the branch/output in your node reference.

That is, to access the output labeled “discover” (index 0), you would use expression $("Switch").all(0) or just $(“Switch”).all() as the first branch (index 0) is by default. Likewise the data from the output “archive” (index 1) would be accessed with $("Switch").all(1).

This alone, though, will not work for you and you need to do some extra data transformation. From your flow in Shopify node you use Switch to access the playlist ID. However, it semantically contradicts the nature of the data $("<node-name>").all(branchIndex?, runIndex?) as it is a list of items grouped by the node output. You would need split out the items first by referencing the Switch in the Code node (not in Shopify node), which also needs to be executed just once (!), as shown below (the improvised flow you are building).

3 Likes

@ihortom, thank you for your insight! It must’ve been because it was late last night that I completely missed the reference of the branchIndex parameter of the .all() method within the docs page :upside_down_face: I appreciate the help, thank you again!

1 Like

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