Transform array in Function

Hello,

I am trying to convert an array to send the item as a number to an API. I get the data from a Modbus connector and receive it this way:

image

I have tried several functions to get the data point (509) and send it as a number to the API

var arrData = msg.payload; 
msg.payload = String(arrData[0]);
return msg;

I have no clue how I can send only the number 509 to the API as a number

In JavaScript, you can call .toString() on a number...

E.g...

var arrData = msg.payload; 
msg.payload = arrData[0].toString();
return msg;

Alternatively, just add ""

var arrData = msg.payload; 
msg.payload = arrData[0] + "";
return msg;

Better still...

msg.payload = msg.payload[0].toString();
return msg;

Thanks! I have tried all three of them in the Function, but I get this message in the debug window:

TypeError: Cannot read property 'toString' of undefined

In which case the data is not as described.

Use debug nodes to check what is going into the function.

This is what I see in the debug mode of the msg:

Well I'm sure you only moved the function temporary but it's not connected. Also, what do you have in the function?

Sorry to barge in .. could the error be because of the first debug message (which is not an array)?

Maybe try something like this:

if( Array.isArray(msg.payload) ){
    msg.payload = msg.payload[0].toString();
    return  msg;
}else{
    node.error("Oops, not an array.")
}

However, you said you need to pass a number to the API. Why do we need .toString() then?

The node that 1st message came from is different to the 2 below
image

However, as you say, it is always good practice to check the type (as many nodes will send empty or odd results upon failure).

To be follow up to your reply, something like below would be another belt + brace...

if( Array.isArray(msg.payload) && msg.payload.length ) {
    msg.payload = msg.payload[0].toString();
    return  msg;
}else{
    node.error("msg.payload is not a valid array", msg); //always include the msg so that the catch node can catch the error
}

Yes, so I can check the direct output of the Modbus connector. This is how the function looks now

if( Array.isArray(msg.payload) && msg.payload.length ) {
    msg.payload = msg.payload[0].toString();
    return  msg;
}else{
    node.error("msg.payload is not a valid array", msg); //always include the msg so that the catch node can catch the error
}

Gives now the error that the array is not valid. I have really no clue what is happening, because if I output directly the modus to the msg payload the debug window says it's a array

Leave the function node attached & add a 2nd debug node. Connect that to the same output that the function node is connected to on the modbus node (i.e. 2 wires from same output pin, 1 already going to function, other going to the newly added debug node) - what do you see.

I suspect you are using a different modbus output pin to monitor the debug output to what you connect the function (hint, the 2 outputs of the modbus output different objects- you can verify that by having 2 debugs, 1 on each output of the modbus node)

Thanks! That was the trick, I was using the wrong output.

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