# Function node weirdness?

**URL:** <https://discourse.nodered.org/t/function-node-weirdness/77440>\
**Category:** General\
**Tags:** function-node, uibuilder\
**Created:** [10 April 2023 22:29 UTC](https://discourse.nodered.org/t/function-node-weirdness/77440 "2023-04-10T22:29:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 April 2023 22:29 UTC](https://discourse.nodered.org/t/function-node-weirdness/77440/1 "2023-04-10T22:29:19Z")

</div>

OK, here's a weird one that I'm too tired to try and work out today.

The following three different inputs SHOULD be identical. Attaching debug nodes shows that they are. However, they generate DIFFERENT outputs from `uib-element` which should be impossible.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f24b0f0d71b63487b239862cf3f831be579e838a.png)

```auto
[{"id":"c405521ed73e5ed2","type":"inject","z":"a70b22bf53a7ea9c","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":570,"y":780,"wires":[["9ed45e7c7100614e"]]},{"id":"9ed45e7c7100614e","type":"function","z":"a70b22bf53a7ea9c","name":"function 11","func":"msg.payload = [\n {\n \"name\": \"dale\",\n \"age\": 14\n },\n {\n \"name\": \"guy\",\n \"age\": 35\n }\n]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":780,"wires":[["6293216dfdec5d13"]]},{"id":"6293216dfdec5d13","type":"uib-element","z":"a70b22bf53a7ea9c","name":"","topic":"","elementtype":"table","parent":"body","parentSource":"","parentSourceType":"str","elementid":"testme","elementId":"","elementIdSourceType":"str","heading":"","headingSourceType":"str","headingLevel":"h2","position":"last","positionSourceType":"str","confData":{},"x":1010,"y":780,"wires":[["addc2de924c739c2"]]},{"id":"c5919dceea24d0a1","type":"inject","z":"a70b22bf53a7ea9c","name":"inject json","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{ \"name\": \"dale\", \"age\": 14 }, { \"name\": \"guy\", \"age\": 35 }]","payloadType":"json","x":580,"y":860,"wires":[["e1fd900a801db85f"]]},{"id":"cd4ab4eb8197fd03","type":"change","z":"a70b22bf53a7ea9c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{ \"name\": \"dale\", \"age\": 14 }, { \"name\": \"guy\", \"age\": 35 }]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":820,"wires":[["6293216dfdec5d13"]]},{"id":"2b2eee0c63eb5ee0","type":"inject","z":"a70b22bf53a7ea9c","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":570,"y":820,"wires":[["cd4ab4eb8197fd03"]]},{"id":"e1fd900a801db85f","type":"junction","z":"a70b22bf53a7ea9c","x":840,"y":860,"wires":[["6293216dfdec5d13"]]}]

```

The bottom two both generate the correct output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/adef1dff3cd55dd7fd439f859a7a9e0fd4f3d607.png)

The function node version generates:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fb120e07977a67b656bf9584a9c06aa3cdf758af.png)

As I say, they all produce an identical msg to feed into the `uib-element` node (I think - unless I'm going mad, but I compared the 2 debug outputs using a diff program).

Thanks to Rami Yeger over on YouTube for pointing this out.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 April 2023 23:30 UTC](https://discourse.nodered.org/t/function-node-weirdness/77440/2 "2023-04-10T23:30:28Z")

</div>

OK, so I can't sleep until I know more!!

Turns out that `msg.payload instanceof Object` is FALSE if the payload was created in a function node. It is TRUE if the payload was created in an inject or change node set to JSON input.

I've changed the code to `if (!(Array.isArray(msg.payload) || typeof msg.payload === 'object')) msg.payload = [msg.payload]`.

I will need to publish a new version tomorrow. It will be v6.4.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 June 2023 23:31 UTC](https://discourse.nodered.org/t/function-node-weirdness/77440/3 "2023-06-09T23:31:05Z")

</div>

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