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

Unexpected return value when expression uses an array length in a function #5226

Closed
NoOverflow opened this issue Jan 23, 2023 · 5 comments
Closed
Labels
in linear Issue or PR has been created in Linear for internal review

Comments

@NoOverflow
Copy link

NoOverflow commented Jan 23, 2023

Hi, I'm not sure if this is a duplicate (I've looked into open issues but couldn't find a close match), or if this issue comes from a misunderstanding on how to use n8n but I still find it weird.

Describe the bug

When using the length of an array set in a previous node, in a function (such as Math.floor), in an inline expression, the return value will always be equal to the actual length no matter what the expression is supposed to do. Hardcoding the length results in expected behaviour.

To Reproduce

(Example workflow provided as attachment for convenience)

  1. Create a workflow with an array set as test value named my_array
  2. Create a "Set" node that set a variable called index from the following expression:
{{ Math.floor(Math.random() * $json["my_array"].length) }}
  1. Confirm that returned value is always equals to the array length and not what actual JS execution would return
  2. Create another "Set" node that swap the dynamic array length for an hardcoded value using the following expression:
{{ Math.floor(Math.random() * 6) }}
  1. Confirm that the returned value matches what would be expected for this expression computation

Expected behavior

An expression should be able to contain the dynamic length of an array

Environment (please complete the following information):

  • OS: Tested on Ubuntu Linux 22.10, Centos 7
  • n8n Version 0.212.0 (Latest on docker)
  • Node.js Version (Matching docker latest)
  • Database system SQLite
  • Operation mode own
  • Tested on both Docker install and Openshift

Additional context
I attached the test workflow for convenience, however it's lacking the n8n test data, you can just add a dummy array containing a few values.

workflow.zip

@NoOverflow
Copy link
Author

Posting additional screenshot that helps understand the issue (Return value here should be 800 + 6 = 806)
image

@NoOverflow NoOverflow changed the title Unexpected return value when expression uses an array length Unexpected return value when expression uses an array length in a function Jan 23, 2023
@Joffcom
Copy link
Member

Joffcom commented Jan 23, 2023

Hey @NoOverflow,

Thanks for reporting this one, I must be missing something as I can see the result is wrong in your screenshot but when I run a workflow to try and reproduce it is running as expected.

...

As I was about to post this I just noticed I was using .length() and that was working but .length is failing.

image

I have created N8N-6026 as our internal ticket for tracking this one.

@Joffcom Joffcom added the in linear Issue or PR has been created in Linear for internal review label Jan 23, 2023
@NoOverflow
Copy link
Author

Hey @Joffcom, thanks for checking it so quickly. Can confirm, length() does work (I don't know why I didn't try that first...)

@Joffcom
Copy link
Member

Joffcom commented Jan 24, 2023

Hey @NoOverflow,

Good news, The fix for this has been merged (#5232) and will be available in the next release. I am going to get this one marked as closed for now 👍🏻 thanks again for the report.

@Joffcom Joffcom closed this as completed Jan 24, 2023
@Joffcom Joffcom added the Upcoming Release Will be part of the upcoming release label Jan 24, 2023
@janober
Copy link
Member

janober commented Jan 27, 2023

Fix got released with [email protected]

@janober janober removed the Upcoming Release Will be part of the upcoming release label Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in linear Issue or PR has been created in Linear for internal review
Projects
None yet
Development

No branches or pull requests

3 participants