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

**URL:** <https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error/32155>\
**Category:** General\
**Created:** [28 August 2020 14:50 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error/32155 "2020-08-28T14:50:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhanush12](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dhanush12](https://discourse.nodered.org/u/dhanush12)\
**Post date:** [28 August 2020 14:50 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error/32155/1 "2020-08-28T14:50:26Z")

</div>

Please help me to resolve this issue, that would be a great support of yours to me !!!  
var grad=msg.payload  
if (grad\>85)  
{  
msg.grade="S";  
}  
else if (grad\<=85 && grad\>80)  
{  
msg.grade="A";  
}

else if (grad\<=80 && grad\>70)  
{  
msg.grade="B";

```
}

```

else if (grad\<=70 && grad\>60)  
{  
msg.grade="C"

```
}

```

else if (grad\<=60 && grad\>50)  
{  
msg.grade="D";

```
}

```

else if (grad\<=50 && grad\>40)  
{  
msg.grade="E";

```
}

```

else {  
msg.grade="Fail";  
}  
return msg.grade;

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 August 2020 14:58 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error/32155/2 "2020-08-28T14:58:15Z")

</div>

As the error says, your function is returning a String object, rather than a message object.

If you look at the end of your function you have:

```auto
return msg.grade

```

Which means you are returning the value of `msg.grade` - a String, not a complete message object.

Change that to:

```auto
return msg;

```

---

<div class="post-metadata">

**Author:** ![dhanush12](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dhanush12](https://discourse.nodered.org/u/dhanush12)\
**Post date:** [29 August 2020 02:29 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error/32155/3 "2020-08-29T02:29:25Z")

</div>

I tried return msg; instead of ms.grade , getting value same value in msg.payload instead of string

---

<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:** [29 August 2020 06:37 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error/32155/4 "2020-08-29T06:37:19Z")

</div>

You are putting the value in msg.grade not msg.payload. Add a debug node showing the output and set it to Show Complete Message and you will see what is there. Also I suggest watching the videos 'node red essentials' which will save you a lot of time. Also read the the docs Working With Messages and the one on Writing Functions.

---

<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:** [28 October 2020 06:37 UTC](https://discourse.nodered.org/t/function-tried-to-send-a-message-of-type-string-error/32155/5 "2020-10-28T06:37:34Z")

</div>

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