I have a function node that creates a msg.payload and msg.topic.
I would like to output that msg to output 1 of the function node.
So far no problem.
Within that same function I would like to create a second msg.payload and msg.topic which should be output to output 2.
So something like
for (let i = 0; i < devices.length; i++) {
for (let j = 0; j < entities.length; j++) {
msg.topic = "topic 1" + [i]
msg.payload = "payload 1" + [i]
node.send([msg, null])
}}
for (let i = 0; i < devices.length; i++) {
for (let j = 0; j < entities.length; j++) {
msg.topic = "topic of message 2" + [i]
msg.payload = "payload 2" + [i]
node.send([null, msg])
}}
Is that possible?
I amtrying to avoid creating the first message, outputting it via node.send([msg, null]) and then creating the second and outputting it to out 2 due to the asyncronous behaviour of node-red (since in both cases I will be creating msg.payload and msg.topic and I do not want them to mix and match).
you can manipulate the "package" during its transport through other Nodes, or create a new package, by simply defining a new object (you don't have to call it msg) but Node RED will send your package wrapped in a property called msg for the next node
but each node downstream will still receive an msg object - but each being not related (as you are creating new objects for each output)
And in that, any properties you added (payload, topic. etc etc)
msg.payload
msg.topic
if you don't create a new object (package) - all you are doing is manipulating the currentmsg flowing through
move the object creation inside your loop.
else the next iteration will just modify the previous msg1, and msg2 object that was sent on the previous iteration.
if you manipulate something that was already sent, the nodes dealing with the previous iteration will see the modifications - you probably don't want that, so create a new object(s) for each loop