New CODE node --- changes

Describe the issue/error/question

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.

Old code:

New code (doesn’t work):

  • How can I read and learn about the changes?
  • Where I can find documentation about learning JS for n8n (with this new code node)?
  • What will be the code for same functionality in this case?

Thanks!!

Hi @MaaPer,
in your case the code node could look something like this.

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.

1 Like

This new “Code” node is very confusing. I’m not able to point to items and the documentation doesn’t help either :frowning:

3 Likes

@marcus, I tried following your instructions, but it doesn’t work. Have you updated the documentation yet?

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]”

1 Like

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.

Here is an example workflow.

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.

Not really helping here. How do I point to a specific key?

I am not sure what you mean. Can you elaborate a bit more what you are trying to achieve?

I want to parse a specific string but I cannot even get the string in this new “Code”

Here is an example of where I try to get the string.

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.

Thank you :+1:

None of this is in the documentation and the suggestions in the gui doesn’t help either. Just some feedback.

1 Like

@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”]

Hey @wallinex,

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;

Does this help at all?

1 Like

Thank you! I will test this right away.

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;

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