# Problem inserting object into template node

**URL:** <https://discourse.nodered.org/t/problem-inserting-object-into-template-node/54103>\
**Category:** General\
**Created:** [22 November 2021 12:13 UTC](https://discourse.nodered.org/t/problem-inserting-object-into-template-node/54103 "2021-11-22T12:13:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nerevar](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@nerevar](https://discourse.nodered.org/u/nerevar)\
**Post date:** [22 November 2021 12:13 UTC](https://discourse.nodered.org/t/problem-inserting-object-into-template-node/54103/1 "2021-11-22T12:13:04Z")

</div>

Hi,

I'm having a problem in inserting an object to the template node (not ui\_template).  
I want to export charts to html, but the problem is I need to do it dynamicaly. Static example works with no problems.  
previously I used to insert variables inside the body tags using `{{{variable}}}`, and arrays using inside script tags `[{{{array}}}]`. I've even try using `{{#payload}} {{payload.something}} {{/payload}}`, but it still doesn't work.  
Anyone had similar case?

Here are 2 examples of which one works and another not.

static:  
[flows (66).json](https://discourse.nodered.org/uploads/short-url/49WPz5jX0klMKFGKomujiAhcHxp.json) (5.3 KB)

dynamic:  
[flows (67).json](https://discourse.nodered.org/uploads/short-url/dC6sWEX6k5k1uSmhUGaNt2RItnq.json) (5.2 KB)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 November 2021 12:25 UTC](https://discourse.nodered.org/t/problem-inserting-object-into-template-node/54103/2 "2021-11-22T12:25:39Z")

</div>

The template node will output msg.payload as it is a js object it can not be used in the mustache template. The output would be [object][object].

Set msg.payload as as json string  
e.g

```auto
[{"id":"f164ee19.5da43","type":"inject","z":"c791cbc0.84f648","name":"load","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":"1","topic":"","payload":"","payloadType":"date","x":120,"y":320,"wires":[["6a268f39.2a5b1"]]},{"id":"6a268f39.2a5b1","type":"function","z":"c791cbc0.84f648","name":"data","func":"msg = {};\nmsg.payload = `{\n \"$schema\": \"https://vega.github.io/schema/vega/v5.json\",\n \"description\": \"A basic line chart example.\",\n \"width\": 500,\n \"height\": 200,\n \"padding\": 5,\n \n \"signals\": [\n {\n \"name\": \"interpolate\",\n \"value\": \"linear\",\n \"bind\": {\n \"input\": \"select\",\n \"options\": [\n \"basis\",\n \"cardinal\",\n \"catmull-rom\",\n \"linear\",\n \"monotone\",\n \"natural\",\n \"step\",\n \"step-after\",\n \"step-before\"\n]\n }\n }\n ],\n \n \"data\": [\n {\n \"name\": \"table\",\n \"values\": [\n {\"x\": 0, \"y\": 28, \"c\":0}, {\"x\": 0, \"y\": 20, \"c\":1},\n {\"x\": 1, \"y\": 43, \"c\":0}, {\"x\": 1, \"y\": 35, \"c\":1},\n {\"x\": 2, \"y\": 81, \"c\":0}, {\"x\": 2, \"y\": 10, \"c\":1},\n {\"x\": 3, \"y\": 19, \"c\":0}, {\"x\": 3, \"y\": 15, \"c\":1},\n {\"x\": 4, \"y\": 52, \"c\":0}, {\"x\": 4, \"y\": 48, \"c\":1},\n {\"x\": 5, \"y\": 24, \"c\":0}, {\"x\": 5, \"y\": 28, \"c\":1},\n {\"x\": 6, \"y\": 87, \"c\":0}, {\"x\": 6, \"y\": 66, \"c\":1},\n {\"x\": 7, \"y\": 17, \"c\":0}, {\"x\": 7, \"y\": 27, \"c\":1},\n {\"x\": 8, \"y\": 68, \"c\":0}, {\"x\": 8, \"y\": 16, \"c\":1},\n {\"x\": 9, \"y\": 49, \"c\":0}, {\"x\": 9, \"y\": 25, \"c\":1}\n]\n }\n ],\n \n \"scales\": [\n {\n \"name\": \"x\",\n \"type\": \"point\",\n \"range\": \"width\",\n \"domain\": {\"data\": \"table\", \"field\": \"x\"}\n },\n {\n \"name\": \"y\",\n \"type\": \"linear\",\n \"range\": \"height\",\n \"nice\": true,\n \"zero\": true,\n \"domain\": {\"data\": \"table\", \"field\": \"y\"}\n },\n {\n \"name\": \"color\",\n \"type\": \"ordinal\",\n \"range\": \"category\",\n \"domain\": {\"data\": \"table\", \"field\": \"c\"}\n }\n],\n \n \"axes\": [\n {\"orient\": \"bottom\", \"scale\": \"x\"},\n {\"orient\": \"left\", \"scale\": \"y\"}\n],\n \n \"marks\": [\n {\n \"type\": \"group\",\n \"from\": {\n \"facet\": {\n \"name\": \"series\",\n \"data\": \"table\",\n \"groupby\": \"c\"\n }\n },\n \"marks\": [\n {\n \"type\": \"line\",\n \"from\": {\"data\": \"series\"},\n \"encode\": {\n \"enter\": {\n \"x\": {\"scale\": \"x\", \"field\": \"x\"},\n \"y\": {\"scale\": \"y\", \"field\": \"y\"},\n \"stroke\": {\"scale\": \"color\", \"field\": \"c\"},\n \"strokeWidth\": {\"value\": 2}\n },\n \"update\": {\n \"interpolate\": {\"signal\": \"interpolate\"},\n \"strokeOpacity\": {\"value\": 1}\n },\n \"hover\": {\n \"strokeOpacity\": {\"value\": 0.5}\n }\n }\n }\n]\n }\n ]\n \n }\n}`\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":320,"wires":[["97e59b4b.7e8978"]]},{"id":"97e59b4b.7e8978","type":"template","z":"c791cbc0.84f648","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<head>\n <meta charset=\"utf-8\">\n <script src=\"https://cdn.jsdelivr.net/npm/vega@5\"></script>\n <script src=\"https://cdn.jsdelivr.net/npm/vega-embed@6\"></script>\n</head>\n<body>\n <div id=\"vis\" style=\"height:449px\">\n </div>\n</body>\n<script>\n const spec = {{{payload}}}\n \n \tvegaEmbed(\"#vis\", spec)\n \t// result.view provides access to the Vega View API\n .then(result => console.log(result))\n .catch(console.warn);\n</script>\n\n","output":"str","x":410,"y":320,"wires":[["73230270.560b6c"]]},{"id":"73230270.560b6c","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":360,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![nerevar](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@nerevar](https://discourse.nodered.org/u/nerevar)\
**Post date:** [22 November 2021 12:44 UTC](https://discourse.nodered.org/t/problem-inserting-object-into-template-node/54103/3 "2021-11-22T12:44:42Z")

</div>

Thank you. It works after bit of debugging 👍

---

<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:** [6 December 2021 12:45 UTC](https://discourse.nodered.org/t/problem-inserting-object-into-template-node/54103/4 "2021-12-06T12:45:23Z")

</div>

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