Help with inserting payload in template

Hi. I want to insert payload from an xml in a template node.
I have succseded with a ui form but i don´t know why this is not working?

[{"id":"f53853fa.2a74","type":"template","z":"75a648ae.abdc08","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>","output":"str","x":1200,"y":700,"wires":[["57d01be.b3113e4"]]},{"id":"57d01be.b3113e4","type":"xml","z":"75a648ae.abdc08","name":"","property":"payload","attr":"","chr":"","x":1330,"y":700,"wires":[["5ce3f42f.58da1c"]]},{"id":"aace4f7e.4f8b9","type":"inject","z":"75a648ae.abdc08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1030,"y":700,"wires":[["f53853fa.2a74"]]},{"id":"51b231a1.24083","type":"template","z":"75a648ae.abdc08","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"My Name is {{{payload[0]}}} {{{payload[1]}}}","output":"str","x":1800,"y":700,"wires":[["9f57c458.80fc88"]]},{"id":"42560560.773f7c","type":"join","z":"75a648ae.abdc08","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1650,"y":700,"wires":[["51b231a1.24083"]]},{"id":"9f57c458.80fc88","type":"debug","z":"75a648ae.abdc08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1950,"y":700,"wires":[]},{"id":"5ce3f42f.58da1c","type":"function","z":"75a648ae.abdc08","name":"Testing","func":"var msg1 = { payload:msg.payload.note.to[0] };\nvar msg2 = { payload:msg.payload.note.from[0] };\nvar msg3 = { payload:msg.payload.note.heading[0] };\nvar msg4 = { payload:msg.payload.note.body[0] };\nreturn [ msg1, msg2, msg3, msg4 ];\n","outputs":4,"noerr":0,"initialize":"","finalize":"","x":1500,"y":700,"wires":[["42560560.773f7c"],["42560560.773f7c"],["42560560.773f7c"],["42560560.773f7c"]]}]

Show us what is going into the template and what comes out. That is more important than the flow, initially.

The reason it's not working is template does not accept [0], it would be payload.0.0 etc

here is an example one simple way and your way with function and join node, hope it helps

[{"id":"5540a6e2.39cfc","type":"inject","z":"57675e72.26a0d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":4880,"wires":[["59eaf72a.f966a8"]]},{"id":"59eaf72a.f966a8","type":"template","z":"57675e72.26a0d8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>","output":"str","x":310,"y":4880,"wires":[["46981544.d0d3ec"]]},{"id":"46981544.d0d3ec","type":"xml","z":"57675e72.26a0d8","name":"","property":"payload","attr":"","chr":"","x":440,"y":4880,"wires":[["b32682e1.41b188","bd8302b8.a4ca58"]]},{"id":"b32682e1.41b188","type":"template","z":"57675e72.26a0d8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"My Name is {{{ payload.note.to }}} {{{payload.note.from}}}","output":"str","x":590,"y":4880,"wires":[["8d869a47.30354"]]},{"id":"bd8302b8.a4ca58","type":"function","z":"57675e72.26a0d8","name":"Testing","func":"var msg1 = { payload:msg.payload.note.to };\nvar msg2 = { payload:msg.payload.note.from };\nvar msg3 = { payload:msg.payload.note.heading };\nvar msg4 = { payload:msg.payload.note.body };\nreturn [ msg1, msg2, msg3, msg4 ];\n","outputs":4,"noerr":0,"initialize":"","finalize":"","x":600,"y":4800,"wires":[["d0732c01.2e611"],["d0732c01.2e611"],["d0732c01.2e611"],["d0732c01.2e611"]]},{"id":"8d869a47.30354","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":4880,"wires":[]},{"id":"d0732c01.2e611","type":"join","z":"57675e72.26a0d8","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":750,"y":4820,"wires":[["74eae1b.5892e2"]]},{"id":"74eae1b.5892e2","type":"template","z":"57675e72.26a0d8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"My Name is {{{ payload.0.0 }}} {{{payload.1.0}}}","output":"str","x":890,"y":4820,"wires":[["8d869a47.30354"]]}]

Note the speed difference.

[edit]
there is a slight error in my code, in the template is should be payload.note.to.0 not payload.note.to and the same with
payload.note.tfrom it works the without the 0 as it displays the array with single item.

Thanks!! Works great!

/Regards Niclas

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