Using Array in mustache format

Hello guys,
This is my first post on the forum. I want to print a highchart
Problem : I am facing issue with use of array in mustache format of template node.
If my msg.payload = [[0,1],[2,3]]
this turns {{payload}} into 0,1,2,3 removing any character in payload just leaving comma


[
    {
        "id": "78a6fa27.cac624",
        "type": "function",
        "z": "4d76d926.b15d48",
        "name": "Object to Array",
        "func": "\n\tmsg.payload =   [[110, 1],[1, 2],[2, 10]]\n\t\n\t\treturn msg",
        "outputs": 1,
        "noerr": 0,
        "x": 440,
        "y": 1940,
        "wires": [
            [
                "9cab8967.24c438",
                "63c23958.bb1fc8"
            ]
        ]
    },
    {
        "id": "9cab8967.24c438",
        "type": "template",
        "z": "4d76d926.b15d48",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "<!DOCTYPE html>\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta charset=\"utf-8\" />\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n<script src=\"https://code.highcharts.com/stock/highstock.js\"></script>\n<script src=\"https://code.highcharts.com/stock/modules/data.js\"></script>\n<script src=\"https://code.highcharts.com/stock/modules/exporting.js\"></script>\n<script src=\"https://code.highcharts.com/stock/modules/export-data.js\"></script>\n\n</head>\n<body>\n<script src=\"https://code.highcharts.com/highcharts.js\"></script>\n<div id=\"container\"></div> \n<script>\n                            \n    Highcharts.stockChart('container', {\n\n        title: {\n            text: 'AAPL Stock Price'\n        },\n\n        series: [{\n            name: 'AAPL',\n            // data: {{payload}},\n            data:  [[0,1],[1,2],[2,10]],\n            tooltip: {\n                valueDecimals: 2\n            }\n        }],\n    \n  rangeSelector: {\n    enabled: true\n  },\n  });\n </script>\n</body>\n</html>\n",
        "output": "str",
        "x": 720,
        "y": 1920,
        "wires": [
            [
                "b254c6a7.1b5818"
            ]
        ]
    },
    {
        "id": "7d99e933.ac36b8",
        "type": "http in",
        "z": "4d76d926.b15d48",
        "name": "",
        "url": "/chart123",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 250,
        "y": 1940,
        "wires": [
            [
                "78a6fa27.cac624"
            ]
        ]
    },
    {
        "id": "b254c6a7.1b5818",
        "type": "http response",
        "z": "4d76d926.b15d48",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 710,
        "y": 2020,
        "wires": []
    },
    {
        "id": "63c23958.bb1fc8",
        "type": "debug",
        "z": "4d76d926.b15d48",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 580,
        "y": 2100,
        "wires": []
    }
]
1 Like

You are outputting as plain text so it will flatten everything including the array to convert it to a string. The same thing happens when you use a toString() method in javascript. I’m not sure there is any way to change this behavior. https://www.w3schools.com/jsref/jsref_tostring_array.asp
as at least in javascript this is expected for an array to string conversation.

Johannes

So i think I found a solution:
You can get around the behavior of an array to string conversion by converting the array to an JSON array string beforehand.
Just use the JSON.stringify() method in the previous function node.

msg.payload =   [[110, 1],[1, 2],[2, 10]];
msg.payload = JSON.stringify(msg.payload);
return msg

Which gives you a JSON string representation of the array which in turn doesn’t get flattened by the template node as it’s already a string.

4 Likes

Thanks for your quick reply. This is now working.

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