# Tried to send a message of type string (function node)

**URL:** <https://discourse.nodered.org/t/tried-to-send-a-message-of-type-string-function-node/53667>\
**Category:** General\
**Created:** [13 November 2021 08:59 UTC](https://discourse.nodered.org/t/tried-to-send-a-message-of-type-string-function-node/53667 "2021-11-13T08:59:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmejialo](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@fmejialo](https://discourse.nodered.org/u/fmejialo)\
**Post date:** [13 November 2021 08:59 UTC](https://discourse.nodered.org/t/tried-to-send-a-message-of-type-string-function-node/53667/1 "2021-11-13T08:59:14Z")

</div>

Hello, I am writing the following function, when the message enters the node, the debug reports the following error, someone can help me correct it.

"Error": "Function tried to send a message of type string"

The function :

```auto
var status = msg.payload;
var id = global.get ("serial");
var off = 0;
var msg1 = "" + id + "" + "f00" + "" + off + "";
var msg2 = "" + id + "" + "f01" + "" + off + "";
var msg3 = "" + id + "" + "f02" + "" + off + "";

if (state == true) {
    return [msg1, msg2, msg3];
}

```

The idea is each of those messages comes out through an output of the node, and I already have the node configured with 3 outputs.

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [13 November 2021 09:05 UTC](https://discourse.nodered.org/t/tried-to-send-a-message-of-type-string-function-node/53667/2 "2021-11-13T09:05:04Z")

</div>

All Node-RED messages are an object, so you must always return a `msg` object from a function node.

What you most likely want to do is return a msg object with `msg.payload` set to be a string.

```auto
var status = msg.payload;
var id = global.get ("serial");
var off = 0;
var msg1 = { payload: "" + id + "" + "f00" + "" + off + ""};
var msg2 = { payload: "" + id + "" + "f01" + "" + off + ""};
var msg3 = { payload: "" + id + "" + "f02" + "" + off + ""};
 

if (status == true) {
    return [msg1, msg2, msg3];
}

```

Also as a rule you shouldn't be creating new `msg` objects in function nodes, it is best practice to reuse the original input message where possible as the object may be carrying other values that are required down stream on the flow (e.g. the http-in and http-response nodes require that the original `msg` object make it all the way down the flow)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [13 November 2021 10:01 UTC](https://discourse.nodered.org/t/tried-to-send-a-message-of-type-string-function-node/53667/3 "2021-11-13T10:01:58Z")

</div>

> [@hardillb](#):
>
> `var msg1 = { payload: "" + id + "" + "f00" + "" + off + ""};`

What is the purpose of the double 'double' quotes ""?  
Doesn't the following give the same result?

`var msg1 = { payload: id + "f00" + off };`

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [13 November 2021 10:15 UTC](https://discourse.nodered.org/t/tried-to-send-a-message-of-type-string-function-node/53667/4 "2021-11-13T10:15:40Z")

</div>

Probably, I was answering from my phone so copy/pasted and made the minimal edit

---

<div class="post-metadata">

**Author:** ![fmejialo](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@fmejialo](https://discourse.nodered.org/u/fmejialo)\
**Post date:** [13 November 2021 10:35 UTC](https://discourse.nodered.org/t/tried-to-send-a-message-of-type-string-function-node/53667/5 "2021-11-13T10:35:18Z")

</div>

Hello, thank you very much for your answer, it works perfectly for the action I want to perform.

---

<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:** [27 November 2021 10:35 UTC](https://discourse.nodered.org/t/tried-to-send-a-message-of-type-string-function-node/53667/6 "2021-11-27T10:35:36Z")

</div>

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