how to receive two output value in a function node in node-red

280 views
Skip to first unread message

svinot...@gmail.com

unread,
Jan 23, 2018, 8:33:13 AM1/23/18
to Node-RED
In node red application I am used exec node for getting the mac address and same time using sq-lite database for get the values. I need to get the mac database node output and get mac from device node output in Get mac function. Is it possible to get the two output in one function.
qwerty.JPG

Colin Law

unread,
Jan 23, 2018, 10:28:34 AM1/23/18
to node...@googlegroups.com
You can use the join node in manual mode setting it to Create a key value object and send the message after 2 message parts and (probably) Every subsequent message.  Make sure the two inputs have different topics then you will get a payload with the two values which you can access in a function (or other) node.

Colin

On 23 January 2018 at 13:33, <svinot...@gmail.com> wrote:
In node red application I am used exec node for getting the mac address and same time using sq-lite database for get the values. I need to get the mac database node output and get mac from device node output in Get mac function. Is it possible to get the two output in one function.

--
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/18de9e9c-4a65-4764-8b37-9ffaf13bd143%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Grant Parks

unread,
Jan 23, 2018, 3:44:37 PM1/23/18
to Node-RED
One way I do this is to use a node context variable (so far this only works when I'm expecting exactly 2 msg inputs like you describe).

In function node:

const savedData = context.get('saved');
if (savedData) {
    // We now have both msgs
    var mergedData = Object.assign({}, msg.payload, savedData);
    context.set('saved', null);
    // Do what you want with it
    return mergedData;
}
else {
    context.set('saved', msg.payload);

steve rickus

unread,
Jan 23, 2018, 5:51:49 PM1/23/18
to Node-RED
It is generally easier and safer to chain those two functions together into one serial flow -- this avoids having to use context to hold values while waiting for the rest of the information to be generated.

Try calling the exec cmd first -- wire that to a change node, and "Move" msg.payload to somewhere else, like msg.mac -- wire that to the db query, which should put its output into msg.payload. The output msg should now contain msg.mac and msg.payload in a single msg object.

The exception may be when two unrelated, async api calls need to be kicked off in parallel -- especially when one may run much longer than the other. But your two steps should run in milliseconds, not minutes. And since node-red (and nodejs) is single-threaded, trying to run two synch functions in parallel paths does not run any faster anyway.
--
Steve
Reply all
Reply to author
Forward
0 new messages