Pass msg.name to a js variable in core template

#1

I want to pass an array of objects from my flow to a js variable inside core template node and use them as input in a javascript program and it is not working.

my sample flow:

[{"id":"9f68c4b6.99ed08","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"deed6c6d.e34968","type":"http in","z":"9f68c4b6.99ed08","name":"","url":"/hello","method":"get","upload":false,"swaggerDoc":"","x":270,"y":125,"wires":[["e12e0103.c4f09"]]},{"id":"e12e0103.c4f09","type":"function","z":"9f68c4b6.99ed08","name":"function","func":"msg.mya = [{\"apples\":25.3,\"bananas\":25,\"oragnges\":0.1}];\n\nreturn msg;","outputs":1,"noerr":0,"x":435,"y":125,"wires":[["c0dea9ec.e6d348"]]},{"id":"c0dea9ec.e6d348","type":"template","z":"9f68c4b6.99ed08","name":"template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<HTML>\n    <HEAD>\n        <TITLE>Title</TITLE>\n        <script> var myValues ={{mya }}; </script>\n\n    </HEAD>\n\n    <BODY>\n        <H1>This is a Header</H1>\n        <H2>This is a Medium Header</H2>\n\n\n    </BODY>\n\n</HTML> ","x":595,"y":125,"wires":[["a3bdb570.3f4d48"]]},{"id":"a3bdb570.3f4d48","type":"http response","z":"9f68c4b6.99ed08","name":"","statusCode":"","headers":{},"x":750,"y":125,"wires":[]}]

Thanks in advance

#2

Hi,

The mustache syntax used by the Template doesn't know how to write a whole objects into the template.

One way of achieving that would be to JSON encode the property first, so you have a String containing the object data and then inserting that.

Your function would be:

msg.mya = JSON.stringify([{"apples":25.3,"bananas":25,"oragnges":0.1}]);
return msg
#3

It worked!

Thank you very much for your instant reply