Hi, I have been fighting with something and I cannot solve it.
I have a system of reception of messages by MQTT of several sensors. One of the bytes of this message (it is a buffer) indicates the "TYPE" of the message/sensor.
I want to receive that buffer in a function node. That function node has as many outputs as there are types of sensors / messages I have.
That is, the function node must read the payload's byte, identify the type/sensor, and output the payload again by the output corresponding to the type/sensor number. So then, each output manages the payload considering the type of sensor that originally sends the message.
This is the function code:
var maxType = 11;
var type = msg.payload [3];
node.warn (type);
var output = new Array (maxTipo) .fill (null);
node.warn (output);
// output [type] = {payload: msg.payload};
output [type] = msg;
node.warn (output);
return [output];
But everything comes out from the 1st output.
In the console, based on the two node.warn that you see in the code, I get this:
3/20/2020 21: 42: 28node: Switch Type
function: (warn)
[null, null, null, null, null, null, null, null, null, null…]
3/20/2020 21: 42: 29node: Switch Type
function: (warn)
[null, object, null, null, null, null, null, null, null, null ...]
You see that in the second warn, the second element of the array is an object (a msg).
But I don't get what I want, the function sends all those nulls and the object in the 1st output, and not, as it should, output nulls for all the outputs except the 2nd, which should come out with the "object" (the msg )
Anyone think what can be wrong?
I have tried return [[output]]; but it gives error.
Thanks in advance. Nice challenge !!!