Dear team, I’m trying to learn something about using javascript on n8n.
I’m not a programmer so is not being easy for me to learn, however tanks to this community, I learn how to solve some basics scenarios.
Now I realise that there are some changes on the way to write code form previous function and function item to the new code node.
I have one very simple scenario in with I was using function item. Even knowing that the flow is working properly with the old node, I wanted to understand the changes, because I realise that the same code is not working on the new code node.
We are currently in the process of updating our documentation. The most helpful page linked in the code node is Built-in methods and variables to know about things like $input.item.
A helpful feature the code node is auto complection to see what variables and methods you can use.
This documentation makes zero sense. Item linking in the Code node
Tried to recreate it but gets “ERROR: items is not defined. Did you mean $input.all()? [line 3]”
Hey @wallinex,
sorry to hear that you are having trouble. In your example you are just returning “red” which does not work because n8 expects a certain data structure passed through the nodes. I am not sure what you are trying to achieve but in your case it would be best to directly manipulate $input.item.json and return $input.item at the end. This way you can be sure that you are returning an object with the right data structure.
What I am trying to understand is how do I point to different items in Code. I need it to use JS functions to parse the data. But if I cannot even point to the correct string to parse it’s impossible. That’s why I made that dummy example.
If you are using the Code node with Run Once for Each item you should not worry about item linking because the scenario of pairing one input item with one output item is handled automatically.
In your example you are acessing source.string from $item("0").$node["Code1"], that actually works. N8n is giving you an error because you only return that string instead of an object. Here is an example returning your string inside an object.
@marcus how do I do when I what the string from all items not just item 0?
In the dummy case it’s just one item but when you have 100 items and you want to parse each.
e.g $items.$node[“Code1”].json[“source”][“string”].$node[“Code1”].json[“source”][“string”]
You would need to loop over the items, The code node gives an example of a for loop when you first open it.
// Loop over input items and add a new field
// called 'myNewField' to the JSON of each one
for (const item of $input.all()) {
item.json.myNewField = 1;
}
return $input.all();
Based on something like this if using Run Once you would end up with something like…
const endResult = [];
for (const item of $input.all()) {
endResult.push({"manipulate": item.json.source.string});
}
return endResult;
Yes it worked like a charm. This is what I needed to do:
const endResult = [];
var timestamp = [];
var date = [];
var hh = [];
var mm = [];
var ss = [];
var mm = [];
var iso = [];
var data = [];
for (const item of $input.all()) {
timestamp = item.json.myArray[5]
date = timestamp.slice(3,11)+"T"
hh = timestamp.slice(12,14)
mm = timestamp.slice(15,17)
ss = timestamp.slice(18,20)
mm = timestamp.slice(21,24)
iso = date + hh + mm + ss + mm
data = item.json.myArray
endResult.push({iso, data});
}
return endResult;