# Transform array in Function

**URL:** https://discourse.nodered.org/t/transform-array-in-function/51809
**Category:** General
**Created:** [3 October 2021 20:26 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809 "2021-10-03T20:26:29Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![rploeg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rploeg/32/48943_2.png) [@rploeg](https://discourse.nodered.org/u/rploeg)
#### Post date: [3 October 2021 20:26 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/1 "2021-10-03T20:26:29Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0c72d89ff66e385355d1c2cbc4a19ddc03c5454.png)

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

```auto
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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 October 2021 20:31 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/2 "2021-10-03T20:31:23Z")

</div>

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

E.g...

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

```

Alternatively, just add ""

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

```

Better still...

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

```

---

<div class="post-metadata">

### Author: ![rploeg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rploeg/32/48943_2.png) [@rploeg](https://discourse.nodered.org/u/rploeg)
#### Post date: [3 October 2021 20:48 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/3 "2021-10-03T20:48:53Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> 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:

```auto
TypeError: Cannot read property 'toString' of undefined

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 October 2021 20:56 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/4 "2021-10-03T20:56:46Z")

</div>

In which case the data is not as described.

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

---

<div class="post-metadata">

### Author: ![rploeg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rploeg/32/48943_2.png) [@rploeg](https://discourse.nodered.org/u/rploeg)
#### Post date: [3 October 2021 21:04 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/5 "2021-10-03T21:04:47Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9fc731e019629fc0628e7691b6fe559c72cad152.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 October 2021 21:10 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/6 "2021-10-03T21:10:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)
#### Post date: [3 October 2021 21:20 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/7 "2021-10-03T21:20:34Z")

</div>

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

Maybe try something like this:

```auto
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?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 October 2021 22:02 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/8 "2021-10-03T22:02:28Z")

</div>

> [@rko](#):
>
> could the error be because of the first debug message (which is not an array)

The node that 1st message came from is different to the 2 below  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2ff2e665ba429f699ea5cd3e7c510fbc2a21bd89.png)

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...

```javascript
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
}

```

---

<div class="post-metadata">

### Author: ![rploeg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rploeg/32/48943_2.png) [@rploeg](https://discourse.nodered.org/u/rploeg)
#### Post date: [4 October 2021 05:56 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/9 "2021-10-04T05:56:17Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> 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

```auto
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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [4 October 2021 06:12 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/10 "2021-10-04T06:12:56Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![rploeg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rploeg/32/48943_2.png) [@rploeg](https://discourse.nodered.org/u/rploeg)
#### Post date: [4 October 2021 06:36 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/11 "2021-10-04T06:36:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [18 October 2021 06:37 UTC](https://discourse.nodered.org/t/transform-array-in-function/51809/12 "2021-10-18T06:37:12Z")

</div>

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