Read payload from a specific msg.topic

Hello, i have a long waiting question.

If i have many msg.topics with msg.payload each, how cand i read (with a function block) a msg.payload for a specific msg.topic??

Use a join node.
you will find multiple examples here
https://discourse.nodered.org/search?q=join%20messages

Or save inputs to context storage, then you can access anytime
https://nodered.org/docs/user-guide/context


i can use SWITCH, but i need it in a function to use them in some conditions.

If you are just trying to route the message down different wires dependent on the topic then use a Switch node, not a function node.

If you do want to do it in a Function node then you can use a javascript switch statement to test the topic and then, for example, to send it to output 2 use
node.send([null, msg, null])
or you can do something clever with a lookup array containing all the topics and find the topic in the array and use the index to build the array to be returned.


so.... an IF condition for the msg.topic it will be enough? other easy way?

You don't need to use a new message

if (msg.topic == 1) {
 return [msg]
}

if (msg.topic == 2) {
 return [null, msg]
}
1 Like

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