Hi, in order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```
You can edit and correct your post by clicking the pencil icon.
See this post for more details - How to share code or flow json
Also have a look at the node red docs page Writing Functions, that will show you how you can use node.warn()
to display data from your function in the debug pane. That will enable you to work out which bit of your function is not doing what you expected.