Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$evaluateExpression cannot handle arrow function or inner function #1492

Closed
nantoniazzi opened this issue Feb 25, 2021 · 5 comments
Closed

Comments

@nantoniazzi
Copy link
Contributor

nantoniazzi commented Feb 25, 2021

Describe the bug
I'm trying to use arrow function in some expressions (I want to filter elements of my previous node) but I always get an error.
I tried to reproduce it in a Function node.

const data = $evaluateExpression('{{  [1, 2, 3, 4, 5].filter(v => v > 2).join(`,`)  }}')

error: "Expression is not valid: Malformed arrow function parameter list"

it doesn't work for anonymous function either

const data = $evaluateExpression('{{  [1, 2, 3, 4, 5].filter(function(v) { return v > 2}).join(`,`)  }}')

error: "Expression is not valid: missing ) after argument list"

I thought it was related to the riot template engine, but I have not the problem executing those expressions in a standalone riot template installation.

To Reproduce
Run this node:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const data = $evaluateExpression('{{  [1, 2, 3, 4, 5].filter(v => v > 2).join(`,`)  }}')\nreturn [{json: {value: data} }];"
      },
      "name": "DEBUG",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        440,
        300
      ]
    }
  ],
  "connections": {}
}

Expected behavior
The Function node should returns

[{json: {value: '3,4,5'}]

instead it raises an error

Environment (please complete the following information):

  • n8n Version 0.108.0
  • Node.js Version v14.15.5
@janober
Copy link
Member

janober commented Feb 25, 2021

Thanks for reporting but that is sadly a known problem. It originates in the library that n8n uses underneath the hood for the expressions riot-tmpl.

You can reproduce with it with you example like this:

const tmpl = require('riot-tmpl').tmpl;
tmpl('{  [1, 2, 3, 4, 5].filter(v => v > 2).join(`,`)  }', {})

Sadly do we currently not have the resources to fix that bug in that library and I am not even sure if it would get merged as it did not get updated for 4 years. So we would either have to fork it at some point or look for an alternative.

@baflo
Copy link
Contributor

baflo commented Sep 17, 2021

Is there any workaround solution for this?

@janober
Copy link
Member

janober commented Sep 17, 2021

Only using a Function-Node and then reference the value afterward.

@tennox
Copy link
Contributor

tennox commented Aug 28, 2022

Is that also why

{{require('html-entities').encode("<tag>blub")}}

doesn't work?

Or is it worth it to create a separate issue?

@Joffcom
Copy link
Member

Joffcom commented Jan 4, 2024

Hey @nantoniazzi,

I am just going through some older issues and it looks like this was fixed when we changed from Riot to Tournament in 1.9.0

image

For now as this looks to be all good I am going to mark this as closed, If you are still seeing this issue let me know and we can open it again.

@Joffcom Joffcom closed this as completed Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants