Bug in node.send?

I created a stream where the message goes through a loop and inside the loop I make a node.send on each iteration for the next node to store the information in the file, however the information is repeated several times in the file, as can be seen in debug. I made an example model to illustrate my problem, as follows:

[{"id":"4102703d.b8d97","type":"function","z":"f800399f.f91e98","name":"","func":"let test = [0,1,2,3,4,5,6,7,8,9,10]\n\nfor(let i in test){\n    msg.payload = test[i]\n    node.send(msg)\n}\n\nreturn\n","outputs":1,"noerr":0,"x":1570,"y":360,"wires":[["a3ff2780.a7cb88"]]},{"id":"3a6a3bac.bcd8e4","type":"inject","z":"f800399f.f91e98","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"300","x":1340,"y":360,"wires":[["4102703d.b8d97"]]},{"id":"a3ff2780.a7cb88","type":"iconvfile-out","z":"f800399f.f91e98","name":"","filename":"test","appendNewline":true,"createDir":false,"overwriteFile":"false","charset":"UTF-8","x":1790,"y":320,"wires":[["24447700.93a97a"]]},{"id":"24447700.93a97a","type":"debug","z":"f800399f.f91e98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1950,"y":320,"wires":[]}]

I guess you change the same msg in every iteration - clone the msg before altering it.

RED.util.cloneMessage(msg);

Cloning the message as you suggested it works, but when using the node.send it would be advisable to clone the message to not have this type of problem?

Cloning a message can take time and would not be necessary if you were generating new messages, so is not required in all cases. Thus we don’t mandate it.

2 Likes

hi @dceejay - sorry to revive this thread, but could you pls confirm if node.send(msg) clones the message in the latest version by default ? my current tests "suggest" that the behavior of node.send(msg) clones the message regardless. In my custom node i would like to control the cloning of msg myself and so do not want to add an extra overhead by a redundant clone operation.

If there is only one output or only one wire attached it doesn't clone. I.E. the first wire isn't cloned. The rest are.

@dceejay thanks for clarifications..