Mustache template can only display info from one object in payload

Hi!

I tried to make simple RSS Reader but i encounter the problem where from combined payload i can't use info for dropdown menu filters based on info from sqlite3 database. If i use feeds then no search fields but when i use keywords then i get dropdown values.
Feeds
image
Keywords
image

Payload

{"feeds":[{"name":"PC"},{"name":"Saab"},{"name":"Audi"},{"name":"Motocikli"}],"keywords":[{"rowid":1,"expression":"7800x3d","urlname":"","amount_of_money":250},{"rowid":2,"expression":"5800x3d","urlname":"","amount_of_money":200},{"rowid":3,"expression":"4790","urlname":"200","amount_of_money":200},{"rowid":4,"expression":"A7","urlname":"250","amount_of_money":2000}]}

I added flow for checkup

[
    {
        "id": "c7526823b578902a",
        "type": "http in",
        "z": "6ae25f92b98f01af",
        "name": "",
        "url": "/monitor/keyword",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 180,
        "y": 380,
        "wires": [
            [
                "94cfdabb822422af"
            ]
        ]
    },
    {
        "id": "94cfdabb822422af",
        "type": "sqlite",
        "z": "6ae25f92b98f01af",
        "mydb": "368ac54f1f670f55",
        "sqlquery": "fixed",
        "sql": "SELECT rowid, * FROM keyword;",
        "name": "db_keywords",
        "x": 430,
        "y": 380,
        "wires": [
            [
                "95a11f7ac06cafd7"
            ]
        ]
    },
    {
        "id": "deb19e11db635174",
        "type": "sqlite",
        "z": "6ae25f92b98f01af",
        "mydb": "368ac54f1f670f55",
        "sqlquery": "msg.topic",
        "sql": "",
        "name": "db_feeds",
        "x": 800,
        "y": 440,
        "wires": [
            [
                "3a86c814640813ee"
            ]
        ]
    },
    {
        "id": "3a86c814640813ee",
        "type": "function",
        "z": "6ae25f92b98f01af",
        "name": "combine payloads",
        "func": "msg.payload = {\n    feeds: msg.payload.toString,\n    keywords: msg.keywords\n};\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1010,
        "y": 440,
        "wires": [
            [
                "000a53dc418b20b2",
                "cdbf57ae58f6a689",
                "c4ec1298425a6839"
            ]
        ]
    },
    {
        "id": "cb64af9e2e3e4b2d",
        "type": "http response",
        "z": "6ae25f92b98f01af",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 1450,
        "y": 440,
        "wires": []
    },
    {
        "id": "5888906c887c5c35",
        "type": "http in",
        "z": "6ae25f92b98f01af",
        "name": "",
        "url": "/monitor/keyword",
        "method": "post",
        "upload": false,
        "swaggerDoc": "",
        "x": 180,
        "y": 480,
        "wires": [
            [
                "f69fc6e896b4d6e3"
            ]
        ]
    },
    {
        "id": "f69fc6e896b4d6e3",
        "type": "template",
        "z": "6ae25f92b98f01af",
        "name": "INSERT",
        "field": "topic",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "INSERT INTO keyword (expression, urlname, amount_of_money) VALUES ('{{payload.expression}}', '{{payload.urlname}}', '{{payload.amount_of_money}}')",
        "output": "str",
        "x": 460,
        "y": 480,
        "wires": [
            [
                "867f3b756b1a96d9"
            ]
        ]
    },
    {
        "id": "867f3b756b1a96d9",
        "type": "sqlite",
        "z": "6ae25f92b98f01af",
        "mydb": "368ac54f1f670f55",
        "sqlquery": "msg.topic",
        "sql": "",
        "name": "db",
        "x": 610,
        "y": 500,
        "wires": [
            [
                "94cfdabb822422af"
            ]
        ]
    },
    {
        "id": "bff868e7cddc237b",
        "type": "http in",
        "z": "6ae25f92b98f01af",
        "name": "",
        "url": "/monitor/keyword/delete/:rowid",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 220,
        "y": 540,
        "wires": [
            [
                "f077f85cef0a99a3"
            ]
        ]
    },
    {
        "id": "f077f85cef0a99a3",
        "type": "template",
        "z": "6ae25f92b98f01af",
        "name": "DELETE",
        "field": "topic",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "DELETE FROM keyword WHERE rowid = {{req.params.rowid}}",
        "output": "str",
        "x": 460,
        "y": 540,
        "wires": [
            [
                "867f3b756b1a96d9"
            ]
        ]
    },
    {
        "id": "e0f0302e4d388031",
        "type": "comment",
        "z": "6ae25f92b98f01af",
        "name": "/monitor/keyword",
        "info": "",
        "x": 160,
        "y": 340,
        "wires": []
    },
    {
        "id": "95a11f7ac06cafd7",
        "type": "function",
        "z": "6ae25f92b98f01af",
        "name": "combine results",
        "func": "let keywords = msg.payload;\n\n// Make a copy of the message to query feeds\nlet newMsg = { ...msg, payload: null, topic: null };\nnewMsg.topic = \"SELECT name FROM feed;\";\nnewMsg.keywords = keywords;\nreturn [null,newMsg];",
        "outputs": 2,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 380,
        "wires": [
            [],
            [
                "deb19e11db635174"
            ]
        ]
    },
    {
        "id": "000a53dc418b20b2",
        "type": "debug",
        "z": "6ae25f92b98f01af",
        "name": "debug 26",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "keywords",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1260,
        "y": 520,
        "wires": []
    },
    {
        "id": "c4ec1298425a6839",
        "type": "template",
        "z": "6ae25f92b98f01af",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "<!DOCTYPE html>\n<html>\n<head>\n    <link rel=\"stylesheet\" href=\"//cdn.jsdelivr.net/npm/bootswatch@4.5.2/dist/slate/bootstrap.min.css\"\n        integrity=\"sha384-8iuq0iaMHpnH2vSyvZMSIqQuUnQA7QM+f6srIdlgBrTSEyd//AWNMyEaSF2yPzNQ\" crossorigin=\"anonymous\">\n    <title>Keyword Monitor</title>\n</head>\n<body>\n    <div class=\"container\">\n        <h1>Keyword Monitor</h1>\n        <div class=\"row\">\n            <div class=\"col-md-6\">\n                <form method=\"POST\" action=\"/monitor/keyword\">\n                    <label for=\"urlname\">URL Name</label>\n                    <select class=\"form-control\" id=\"urlname\" name=\"urlname\">\n                        {{#feeds}}\n                        <option value=\"{{{name}}}\">{{{name}}}</option>\n                        {{/feeds}}\n                    </select>\n                    <label for=\"urlname2\">URL Name2</label>\n                    <select class=\"form-control\" id=\"urlname2\" name=\"urlname2\">\n                        {{#keywords}}\n                        <option value=\"{{amount_of_money}}\">{{amount_of_money}}</option>\n                        {{/keywords}}\n                    </select>\n                </form>\n            </div>\n        </div>\n    </div>\n</body>\n</html>\n",
        "output": "str",
        "x": 1260,
        "y": 280,
        "wires": [
            [
                "cb64af9e2e3e4b2d"
            ]
        ]
    },
    {
        "id": "cdbf57ae58f6a689",
        "type": "debug",
        "z": "6ae25f92b98f01af",
        "name": "debug 27",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1260,
        "y": 360,
        "wires": []
    },
    {
        "id": "368ac54f1f670f55",
        "type": "sqlitedb",
        "db": "/etc/sqlite/monitor.db",
        "mode": "RWC"
    }
]

Not sure what your issue is as html created with mustache displays fine in my browser

p.s. The forum provides a way of sharing flow json, it is preferred if you use this method.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Also most will not have access to your data base so it is helpful if you provide a sample of the sql responses for testing, Use debug nodes to get this data in the correct format

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

[edit]
Your issues is that the keywords and feeds are in payload and there is a seperate msg.keywords. Set your debug to see complate message object.

the template should be

<select class="form-control" id="urlname" name="urlname">
                        {{#payload.feeds}}
                        <option value="{{name}}">{{name}}</option>
                        {{/payload.feeds}}
                    </select>
                    <label for="urlname2">URL Name2</label>
                    <select class="form-control" id="urlname2" name="urlname2">
                        {{#payload.keywords}}
                        <option value="{{expression}}">{{expression}}</option>
                        {{/payload.keywords}}
                    </select>
1 Like

First!
Thanks for formatting tips for forum posts @E1cid.
Second!
Thanks for solution
#payload.feeds did the trick. Had same problem with JSONata Expression with $join(payload.expression, "|") and forgot about that.
Still weird that half of payload reads and half don't without #payload.*

Not weird as there is a separate msg.keywords, which you create in first function. Check the debug of this example and you will see it. That is why only one dropdown was working.

[{"id":"f4af9368ec715a01","type":"http in","z":"d1395164b4eec73e","name":"","url":"/monitor/keyword","method":"get","upload":false,"swaggerDoc":"","x":400,"y":4900,"wires":[["31aca21b30693e89"]]},{"id":"31aca21b30693e89","type":"change","z":"d1395164b4eec73e","name":"simulate sql 1","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"expression\":\"12345\"},{\"expression\":\"67890\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":4840,"wires":[["95a11f7ac06cafd7"]]},{"id":"95a11f7ac06cafd7","type":"function","z":"d1395164b4eec73e","name":"combine results","func":"let keywords = msg.payload;\n\n// Make a copy of the message to query feeds\nlet newMsg = { ...msg, payload: null, topic: null };\nnewMsg.topic = \"SELECT name FROM feed;\";\nnewMsg.keywords = keywords;\nreturn [null,newMsg];","outputs":2,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":4840,"wires":[[],["5f044fa7ab40c7b8"]]},{"id":"5f044fa7ab40c7b8","type":"change","z":"d1395164b4eec73e","name":"simulate sql2","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\"name\":\"pc\"},{\"name\":\"saab\"}]","tot":"json"},{"t":"set","p":"keywords","pt":"msg","to":"[{\"expression\":\"12345\"},{\"expression\":\"67890\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":4840,"wires":[["3a86c814640813ee"]]},{"id":"3a86c814640813ee","type":"function","z":"d1395164b4eec73e","name":"combine payloads","func":"msg.payload = {\n    feeds: msg.payload,\n    keywords: msg.keywords\n};\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1030,"y":4840,"wires":[["c0907805a667e0df","c4ec1298425a6839"]]},{"id":"c0907805a667e0df","type":"debug","z":"d1395164b4eec73e","name":"debug 2558","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":950,"y":4980,"wires":[]},{"id":"c4ec1298425a6839","type":"template","z":"d1395164b4eec73e","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html>\n<head>\n    <link rel=\"stylesheet\" href=\"//cdn.jsdelivr.net/npm/bootswatch@4.5.2/dist/slate/bootstrap.min.css\"\n        integrity=\"sha384-8iuq0iaMHpnH2vSyvZMSIqQuUnQA7QM+f6srIdlgBrTSEyd//AWNMyEaSF2yPzNQ\" crossorigin=\"anonymous\">\n    <title>Keyword Monitor</title>\n</head>\n<body>\n    <div class=\"container\">\n        <h1>Keyword Monitor</h1>\n        <div class=\"row\">\n            <div class=\"col-md-6\">\n                <form method=\"POST\" action=\"/monitor/keyword\">\n                    <label for=\"urlname\">URL Name</label>\n                    <select class=\"form-control\" id=\"urlname\" name=\"urlname\">\n                        {{#payload.feeds}}\n                        <option value=\"{{name}}\">{{name}}</option>\n                        {{/payload.feeds}}\n                    </select>\n                    <label for=\"urlname2\">URL Name2</label>\n                    <select class=\"form-control\" id=\"urlname2\" name=\"urlname2\">\n                        {{#payload.keywords}}\n                        <option value=\"{{expression}}\">{{expression}}</option>\n                        {{/payload.keywords}}\n                    </select>\n                </form>\n            </div>\n        </div>\n    </div>\n</body>\n</html>\n","output":"str","x":860,"y":4900,"wires":[["e607fce31f023536"]]},{"id":"e607fce31f023536","type":"http response","z":"d1395164b4eec73e","name":"","statusCode":"","headers":{},"x":990,"y":4900,"wires":[]}]

This is also how i would of presented the issue without using sql nodes.

1 Like

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