Function node weirdness?

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.

[{"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

The function node version generates:
image

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.

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.

3 Likes

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