# Function returns a string instead of an array

**URL:** <https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302>\
**Category:** General\
**Created:** [14 August 2019 06:30 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302 "2019-08-14T06:30:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)\
**Post date:** [14 August 2019 06:30 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302/1 "2019-08-14T06:30:50Z")

</div>

Hi there,  
i m a newbee and try to execute a function code in node red.  
my function is supposed to return me an array but instead i got the message error "Function tried to send a message of type string"

Here is my code

var out = ;

for (var i = 0; i \< msg.payload.length; i++) {  
if (msg.payload[i].col2 !==null)  
{

```
  if (msg.payload[i].col2=="x" )
   {
     var tmp= msg.payload[i].col1;
     out.push(tmp);
   }
}

```

}  
return [out];

so the goal is to go through an csv sheet which has 2 column, the first one containing a list of attribute and the second one containing cross. i want to extract only the column 1 attribute where the corresponding column 2 has a cross

here is a template of my csv sheet  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c34d0e5c84c0ba40acef786c64f6f6e1c7e480bc.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [14 August 2019 06:43 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302/2 "2019-08-14T06:43:14Z")

</div>

Function must return an object. So easiest is just to replace the payload of the existing msg.  
So  
return { payload: out }:

---

<div class="post-metadata">

**Author:** ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)\
**Post date:** [14 August 2019 06:53 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302/3 "2019-08-14T06:53:15Z")

</div>

Hi,  
thanks for your answer.  
but is an array not an object?  
i tried this "return { payload:out };" but it is not returning anything. I guess it is because my current message does not have a payload value?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [14 August 2019 07:01 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302/4 "2019-08-14T07:01:26Z")

</div>

Sorry I need a coffee...  
Should have been

```
msg.payload = out;
return msg;

```

Strictly speaking yes an array is an object, but we mean/need a generic one with properties that can be added and extended.

---

<div class="post-metadata">

**Author:** ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)\
**Post date:** [14 August 2019 07:13 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302/5 "2019-08-14T07:13:04Z")

</div>

Understood thanks.  
but still returning nothing ☹  
i don t know if i miss something else...

var out = ;

for (var i = 0; i \< msg.payload.length; i++) {  
if (msg.payload[i].col2 !==null)  
{  
if (msg.payload[i].col2=="x" )  
{  
var tmp= msg.payload[i].col1;  
out.push(tmp);

```
   }
}

```

}

msg.payload = out;  
return msg;

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 August 2019 07:37 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302/6 "2019-08-14T07:37:33Z")

</div>

> [@Reims](#):
>
> but still returning nothing

I don't see any way that can return nothing. What do you see if you feed it into a debug node ? Also what do you see in a debug node showing the input to the function?  
If you actually mean that the message payload sent is an empty array then probably there are no records that pass the tests.  
You can debug your function using `node.warn()`, for example, at the beginning you could put

```auto
node.warn(`Length is ${msg.payload.length}`)

```

and it will show the length in the debug pane.

---

<div class="post-metadata">

**Author:** ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)\
**Post date:** [14 August 2019 07:39 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302/7 "2019-08-14T07:39:33Z")

</div>

i m sorry,  
i think , i also need a coffee...  
i forgot to connect my debug node.  
now it is working thanks!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 August 2019 07:50 UTC](https://discourse.nodered.org/t/function-returns-a-string-instead-of-an-array/14302/8 "2019-08-14T07:50:02Z")

</div>

You can tidy your function up a bit as follows if you like

```auto
var out = [];

for (var i = 0; i < msg.payload.length; i++) {
if (msg.payload[i].col2 !==null && msg.payload[i].col2=="x" )
{
  out.push( msg.payload[i].col1);
}
msg.payload = out;
return msg;

```

In fact you don't even need the null test as `msg.payload[i].col2` will evaluate to `undefined` if it does not exist so it will fail the test even if it is null. You would need it in some languages but not javascript.
