But basically I can paste the output of the previous node into my function and it works perfectly. But when I try to reference it dynamically via variable, I don’t know how to refer to it because it’s an array of items instead of the usual JSON object.
What is the error message (if any)?
I get many depending on how I try to reference it.
Please share your workflow
won't let me paste the workflow code - says error 403 when I try to do so
Share the output returned by the last node
Trying to get a single text field that concatenates each items field.
@liam thanks for your response.
When I try to paste the workflow it gives me this error (presumably because there’s markdown syntax contained in one of the data fields):
Actually @liam I just realized this situation is precisely what the Aggregate node handles (miracle what a night’s sleep does after banging your head on something). I was able to replace that workflow with an Aggregate step and that gave me exactly what I wanted:
I would still love to know the answer to my question of how to reference an array passed into a code node but this is no longer a blocker for me given I’m able to solve the need using the Aggregate node. Thanks so much for offering to look at it.
Sorry for the delay. Just watched your video again and understand your question better.
It’s just how to use to the items within the code block, right?
First, $ is where pretty much everything is. And now n8n added a lot of autocompleting so just typing $ and following autocomplete will take you pretty far.
Second, console.log() works in n8n code nodes so you can debug and test what is happening in your code. It prints to the regular console.
For example:
$input.all() will return an array of all of the items. From $input.all() you can do anything you want with the data any way you want, use any array methods.
The most common is to perform an action on each item, which is why the default code looks like this:
// Loop over input items and add a new field called 'myNewField' to the JSON of each one
// line below loops over each input item.
// Everything between `{}` will run as many
// times as there are items
for (const item of $input.all()) {
item.json.myNewField = 1; // adds `myNewField` to each item
}
return $input.all(); // Returns everything but keeps the changes
Hopefully that all makes sense. Typically it’s better to avoid code nodes in n8n whenever possible. But sometimes it is just much easier to write something in code.
But still, whenever you go to make a code node first think about if it can be done without the code node first. Code nodes are more resource intensive than the built in nodes
thx @liam incredibly helpful.
I did figure out the $ autocomplete trick by chance. It’s more that I feel like I’m groping around in the dark just trying permutations of stuff when I need to reference something that’s nested in an array that’s wrapped in two layers of JSON. I’ve been building LangChain stuff and when you have the LLM creating JSON output even with the structured output parser I wind up with multiple layers of JSON on the data.
When you learned this stuff was it purely through trial & error or do you recommend like a Udemy course or some other crashcourse thing I could do to get proper foundation in the JSON/Array manipulation aspect?
Believe me I would LOVE to never do anything in the Code node if I could avoid it but given the unpredictability of the output I’m getting out of the AI Agent I have to have some error handling in the code node I think.
On another note: @bartv idea for you guys to empirically determine what new nodes could be popular without even asking people: I would think you could run some analysis on the Code nodes on the hosted version of n8n. Given that people are usually resorting to those only when there’s not a visual dedicated node that does what they need I bet you could learn via the commonalities of everyone’s code nodes what functionality is most missing from the node library? For example I would love one that unwraps n-levels deep nested JSON until you get returned an array of items you’re trying to work with.
Also a webinar I would love would be just basic best practices and practical tips for building pipelines. Some of the unintuitive/useful stuff I’ve learned since building this LangChain app:
You can “pin” data from Code nodes so you don’t have to constantly re-execute everything that preceded that point in your pipeline when testing. You just run the whole thing once then it’s like a chokepoint - you grab the output from the preceding node and then just pin it in a temp code node and that saves you so much on both time and LLM charges.
I was hopeful that I could build things modularly by calling sub worklows with the “Execute Workflow Trigger” but seemingly you can’t pass in private variables using that method so it doesn’t really work for that. I did however figure out how to achieve the same goal via using webhooks and calling them via the HTTPreq node and sending vars via JSON. That combined with the above tip makes it way more sane to develop and you can reuse workflows as components.
I’ve gotten in the habit of putting a “Set” node as the very first step in these modular workflows and just do “Set LocalVars = JSON.body.” By not making direct references to the Webhook node you can do what I’m saying above with the code nodes as chokepoints.
ChatGPT is so incredibly valuable in figuring out the functions for the code node and you can just give it the JSON output of the preceeding node and tell it what you’re trying to accomplish and 9/10 times it’ll get you what you need or at least within striking distance. Great to know that console.log() works for debugging.
Anyways @liam thx for the pointers. If you have any recs on a resource for gaining comfort and proficiency with working with array/JSON data I’d be interested. Otherwise I’m sure it’s a skill you eventually pick up through trial & error over time. cheers
I’m also dumbfounded on how to manipulate my data, I find n8n not being clear and straightforward at all, it’s very difficult to use. I don’t understand the logic, why wouldn’t a previous step return the whole array and return a single object instead is breaking my developer way of reasoning.
Edit: Using const cities = $(Fetch cities’s metadata).all(); // Cities to match with ZRR was the way to go, but really that is counterintuivite that calling .json on an array only returns the first element - wasted 30mn