mysql get data and send it to gui dropdown box

850 views
Skip to first unread message

esc...@missionroom.nl

unread,
Feb 8, 2017, 2:41:01 PM2/8/17
to Node-RED
I am trying to send data from the database to a gui dropdown box.
The proble is that i get an array with arrays and for the dropdown i need an array with object

I use this code to send to the sql node
msg.topic = "SELECT title, id FROM services";

return msg;

runs wel and this gives me
[ { "title": "Thema A", "id": 1 }, { "title": "Thema B", "id": 2 }, { "title": "Thema C", "id": 3 } ]

but what i need is 
[ { "Thema A", 1 }, { "Thema B", 2 }, { "Thema C", 3 } ]


i tried to do this with the code below
var outputArray = [];

for(var i in msg.payload){
    var entryData = [msg.payload[i]['title']];
    for(var attr in msg.payload[i]) {
        if(attr!='title') {
            entryData.push(msg.payload[i][attr])}
            }
    outputArray.push(entryData); } 

var returnMsg={"options":outputArray};
return returnMsg;

That gives me
[ [ "Thema A", 1 ], [ "Thema B", 2 ], [ "Thema C", 3 ] ]

But.... the arrays should be objects to be working with the gui dropdownbox

All tips are welcome!
Thanks!


Nick O'Leary

unread,
Feb 8, 2017, 3:05:48 PM2/8/17
to Node-RED Mailing List
Hi,


The 'map' function is very useful when you have an array of data that you need to transform like this.

msg.options = msg.payload.map(function(el) {
    var result = {};
    result[el.title] = el.id;
    return result;
});
return msg;


Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/10c41f74-6dbb-4ffb-b1e5-a733780245d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

esc...@missionroom.nl

unread,
Feb 8, 2017, 3:22:36 PM2/8/17
to Node-RED
Amazing!
Thanks for your speedy and working reply!!


Op woensdag 8 februari 2017 21:05:48 UTC+1 schreef Nick O'Leary:
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

steve rickus

unread,
Feb 8, 2017, 5:18:54 PM2/8/17
to Node-RED
This same request seems to crop up frequently, so I was thinking that a node to do this would be handy.

Then I remembered that there is some JSONata syntax that does this very simply:  $.{title: id}


Nick, I remember you said we could use that syntax in a few places, but there is no JSONata node, is there? So how can we invoke that "magic" transformation using the core nodes?
--
Steve

Nick O'Leary

unread,
Feb 8, 2017, 5:32:48 PM2/8/17
to Node-RED

As per the release blog post, the change node has jsonata support - it's in the dropdown list of types.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

steve rickus

unread,
Feb 8, 2017, 5:53:30 PM2/8/17
to Node-RED
Ah, brilliant! The change node works nicely, once I modified the expression to be  msg.payload.{title: id}
FWIW, it would help a bit to have an example (maybe this one!) in the sidebar info.

[
    {
        "id": "f7b73b78.3823b8",
        "type": "inject",
        "z": "a896d148.586bc",
        "name": "Array of objects",
        "topic": "",
        "payload": "[ { \"title\": \"Thema A\", \"id\": 1 }, { \"title\": \"Thema B\", \"id\": 2 }, { \"title\": \"Thema C\", \"id\": 3 } ]",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 140,
        "y": 340,
        "wires": [
            [
                "6fa91881.dc6378"
            ]
        ]
    },
    {
        "id": "6fa91881.dc6378",
        "type": "change",
        "z": "a896d148.586bc",
        "name": "build {key: val} array",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "msg.payload.{title: id}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 380,
        "y": 340,
        "wires": [
            [
                "306cc493.091e5c"
            ]
        ]
    },
    {
        "id": "306cc493.091e5c",
        "type": "debug",
        "z": "a896d148.586bc",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 630,
        "y": 340,
        "wires": []
    }
]

Of course, it's probably a toss-up as to whether JSONata syntax or the map(...)  function is more confusing to the uninitiated...
Live and learn!
Reply all
Reply to author
Forward
0 new messages