Using standard javascript Set in code node not working

Using the javascript standard Set class does not seem to work in code nodes:

This gives an empty result:


In this fiddle (pure Javascript, no libraries) it is working as expected:

  • n8n version: 1.38.2
  • Database (default: SQLite): sqlite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ??
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu 22.04

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

@einfachiso , try using this instead

Sets do not integrate with JSON, which is expected to be outputted by n8n node.

1 Like

Thank you for your answer, @ihortom.

It does not work either when I return example.values() however which is an array.

Also when I try to log any content of the Set to console the output is always empty as well.

So apparently adding to a Set does not work in n8n for some reason(?).

You still can use sets

This produces the same result as in the output of the 1st workaround. You would have to convert sets to arrays as in Array.from(set).

2 Likes

Using Array.from(set) is definitely a good solution here.
The underlying issue is that the code node runs JS in a sandbox, and any data that needs to be pulled out of the sandbox has to go through serialization/de-serialization.
The sandbox (just like JSON.stringify) doesn’t know how to serialize Sets and Maps, and converts objects like these to {}.

2 Likes

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