loop for each mysql result

2,326 views
Skip to first unread message

esc...@missionroom.nl

unread,
Feb 28, 2017, 4:46:05 AM2/28/17
to Node-RED
while trying to loop trough my myqsl results i get stuck...
What i wish to do is for each result to trigger the exec node
so if i have 3 results i want to send 3 times exe some.exe - parameter 1, some.exe - parameter 2, some.exe - parameter 3.
so i think i need something like for each object in array?

i tried like this:
var myStringArray = msg.payload;
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
    msg.payload = (myStringArray[i]);
    return msg;
  
}
  
thats not working, i expected 3 sinlge messages...

 but... is it possible to do something like this?
if so, what is my mistake?

Colin Law

unread,
Feb 28, 2017, 4:53:22 AM2/28/17
to node...@googlegroups.com
If you want to return a sequence of messages you can either build an
array of messages and return that, or you can send them one at a time
from within the loop using node.send. I suggest you have a look at
https://nodered.org/docs/writing-functions. In fact you might save
yourself some time if you spend a few hours browsing the docs. There
is loads of useful stuff there.

Colin
> --
> 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.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/0d5dcb6d-5d49-46b7-a210-725c02755422%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

esc...@missionroom.nl

unread,
Feb 28, 2017, 5:00:22 AM2/28/17
to Node-RED
i did and i thought that the script below should help me, but it gave me an error
var outputMsgs = [];
var words = msg.payload.split(" ");
for (var w in words) {
   
outputMsgs.push({payload:words[w]});
}
return [ outputMsgs ];

gave me this:

"TypeError: msg.payload.split is not a function"



Op dinsdag 28 februari 2017 10:53:22 UTC+1 schreef Colin Law:

Colin Law

unread,
Feb 28, 2017, 5:11:09 AM2/28/17
to node...@googlegroups.com
Javascript split works on a string [1], so I suspect your payload is
not a string.

Colin

[1] https://www.w3schools.com/jsref/jsref_split.asp
> https://groups.google.com/d/msgid/node-red/8898dbd7-1d2f-4b63-b9cd-cb5182045342%40googlegroups.com.

esc...@missionroom.nl

unread,
Feb 28, 2017, 5:18:46 AM2/28/17
to Node-RED
i got it...

this did the job:
doSomeAsyncWork(msg, function(result) {
   
node.send({payload:result});
});
return;

So this give me a output for each result
var myStringArray = msg.payload;
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
    var result = (myStringArray[i]);
    
  node.send({payload:result});
}
  return msg;



Op dinsdag 28 februari 2017 11:11:09 UTC+1 schreef Colin Law:

Colin Law

unread,
Feb 28, 2017, 5:22:50 AM2/28/17
to node...@googlegroups.com
You might want return null on the end rather than return msg,
otherwise it will pass on the initial msg as well as all your
individual messages.

Colin
> https://groups.google.com/d/msgid/node-red/2d5826f4-1646-4640-b6f1-5ac6a1742478%40googlegroups.com.

esc...@missionroom.nl

unread,
Feb 28, 2017, 7:49:27 AM2/28/17
to Node-RED
Cool! That did it ;-)

Op dinsdag 28 februari 2017 11:22:50 UTC+1 schreef Colin Law:

steve rickus

unread,
Feb 28, 2017, 12:01:16 PM2/28/17
to Node-RED
IFF your incoming msg.payload is datatype string, another option is the use the split node (char = " ") to break the string into an array of msgs, each with a single word payload...

Dave C-J

unread,
Feb 28, 2017, 1:40:09 PM2/28/17
to node...@googlegroups.com

By default the mysql node will return an array, not a string. (So split is not the thing to use)

sent from phone

steve rickus

unread,
Feb 28, 2017, 1:59:33 PM2/28/17
to Node-RED
Split the array, then split all the strings??? Yeah, not a great solution, since it doesn't handle a regex covering any whitespace/line breaks/punctuation.

So instead, I figured the change node should be able to do this, using the $split() function in a JSONata expression. But when I tried, it seems that the expression validator does not allow a regex as the second argument, even though that is legal in the JSONata docs.

I guess that option is not implemented in the change node yet? Could it be added? Or if it's easier/quicker/better, could the split node separator handle more than 1 character?

Dave C-J

unread,
Feb 28, 2017, 2:09:46 PM2/28/17
to node...@googlegroups.com
Steve, the split node does already handle multiple characters...

[{"id":"bebc7475.8789c8","type":"inject","z":"243f43b.cc59bbc","name":"","topic":"","payload":"hellofooworldfoostuff","payloadType":"str","repeat":"","crontab":"","once":false,"x":184.5,"y":707,"wires":[["2c7ecaa3.b6c786"]]},{"id":"2c7ecaa3.b6c786","type":"split","z":"243f43b.cc59bbc","name":"","splt":"foo","x":374.5,"y":709,"wires":[["135ec8d4.cd8e27"]]},{"id":"135ec8d4.cd8e27","type":"debug","z":"243f43b.cc59bbc","name":"","active":true,"console":"false","complete":"false","x":543,"y":708,"wires":[]}]

steve rickus

unread,
Feb 28, 2017, 2:19:00 PM2/28/17
to Node-RED
Yes, sorry -- badly worded question. I meant that if multiple characters were present, split on any one of them -- like a regex char class [ .,\n\t]
Reply all
Reply to author
Forward
0 new messages