# "Function tried to send a message of type string" error coming

**URL:** <https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065>\
**Category:** Dashboard\
**Created:** [6 August 2019 20:48 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065 "2019-08-06T20:48:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajat](https://avatars.discourse-cdn.com/v4/letter/r/ebca7d/32.png) [@rajat](https://discourse.nodered.org/u/rajat)\
**Post date:** [6 August 2019 20:48 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065/1 "2019-08-06T20:48:03Z")

</div>

Hello to all,  
I am new to the Node Red and trying to code a logic. I am having four Inject buttons and from each button i input one numerical value. These values are fed to the function command which will further generate a payload message of specific color mentioned against the numerical values. Finally i am displaying that color over a indicator made using Template + msg.color command.

Now the only issue here is that i am constantly getting an error message "Function tried to send a message of type string" and my code is not working.

Please help me to resolve this issue, that would be a great support of yours to me !!!

My function code is  
var topic = msg.payload;  
if(topic == 0)  
{  
var color = "lime";  
color = color.toString();  
msg.color = color;  
}  
if(topic == 1)  
{  
var color = "red";  
color = color.toString();  
msg.color = color;  
}  
if(topic == 2)  
{  
var color = "orange";  
color = color.toString();  
msg.color = color;  
}  
if(topic == 3)  
{  
var color = "grey";  
color = color.toString();  
msg.color = color;  
}  
return[msg.color];

---

<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:** [6 August 2019 21:09 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065/2 "2019-08-06T21:09:42Z")

</div>

All function nodes must return messages (that is javascript objects) not simple variables. So you need to use  
`return msg`

---

<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:** [6 August 2019 22:02 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065/3 "2019-08-06T22:02:45Z")

</div>

> [@rajat](#):
>
> var topic = msg.payload;  
> if(topic == 0)  
> {  
> var color = "lime";  
> color = color.toString();  
> msg.color = color;  
> }  
> if(topic == 1)  
> {  
> var color = "red";  
> color = color.toString();  
> msg.color = color;  
> }  
> if(topic == 2)  
> {  
> var color = "orange";  
> color = color.toString();  
> msg.color = color;  
> }  
> if(topic == 3)  
> {  
> var color = "grey";  
> color = color.toString();  
> msg.color = color;  
> }  
> return[msg.color];

Hi, a few pointers...

1. Always put three back ticks ````` on a line above and below your code. Makes it easier to read and in some cases, prevents the forum doing screwy things to your code.
2. This could be easily achieved with standard nodes like the switch and change nodes. You would do yourself a great favour to give it a go.
3. Your code is a bit odd and overly verbose. Below is somewhat simplified.

```auto
var topic = msg.payload;
if(topic == 0){
  msg.color = "lime";
} else if(topic == 1){
  msg.color = "red";
} else if(topic == 2){
  msg.color = "orange";
} else if(topic == 3) { 
  msg.color = "grey";
} 
return msg;

```

1. You might also want to set `msg.color` to something default incase payload isn't 0~3  
  
  
  

One for you to come back to as you get used to programming...

```auto
if(msg.payload >= 0 && msg.payload <= 3){
  msg.color = ["lime","red","orange","grey"][msg.payload];
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![rajat](https://avatars.discourse-cdn.com/v4/letter/r/ebca7d/32.png) [@rajat](https://discourse.nodered.org/u/rajat)\
**Post date:** [7 August 2019 15:45 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065/4 "2019-08-07T15:45:41Z")

</div>

@Steve-Mcl The code works !!! A Heartiest Thanks to you ......  
Please tell me why my return[msg.color] command was wrong.  
Also how msg.color = ["lime","red","orange","grey"][msg.payload]; will provide an output against my defined input.

Again a big thanks to you

---

<div class="post-metadata">

**Author:** ![rajat](https://avatars.discourse-cdn.com/v4/letter/r/ebca7d/32.png) [@rajat](https://discourse.nodered.org/u/rajat)\
**Post date:** [7 August 2019 15:46 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065/5 "2019-08-07T15:46:20Z")

</div>

@Colin Thank you so much !!!

---

<div class="post-metadata">

**Author:** ![Syridian](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/syridian/32/25836_2.png) [@Syridian](https://discourse.nodered.org/u/Syridian)\
**Post date:** [16 July 2020 10:45 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065/6 "2020-07-16T10:45:51Z")

</div>

> [@rajat](#):
>
> Also how msg.color = ["lime","red","orange","grey"][msg.payload]; will provide an output against my defined input.

I'm guessing the `msg.color = ["lime","red","orange","grey"]`part defines msg.color as an array and then the `[msg.payload]` addresses that array with the 0-3 variable you injected. It's a nice neat way to do it and I really appreciate @Steve-Mcl for showing it.

---

<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:** [16 July 2020 10:49 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error-coming/14065/7 "2020-07-16T10:49:06Z")

</div>

> [@Syridian](#):
>
> I'm guessing the `msg.color = ["lime","red","orange","grey"]` part defines msg.color as an array and then the `[msg.payload]` addresses that array with the 0-3 variable you injected

(Almost) correct 🙂

`msg.color` never gets assigned the array, only the element prescribed by `msg.payload`

TBH, it was a solution to a question but not necessarily the best way. Using class names and CSS would be far better solution.
