# Injecting a string in a function not working

**URL:** <https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676>\
**Category:** General\
**Created:** [3 December 2021 15:44 UTC](https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676 "2021-12-03T15:44:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterda](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@peterda](https://discourse.nodered.org/u/peterda)\
**Post date:** [3 December 2021 15:44 UTC](https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676/1 "2021-12-03T15:44:09Z")

</div>

Newbie at work:

In my function node this works:

```auto
msg.payload = Buffer.from('-v.?\r')
return msg;

```

Passing the string from my injection node (as in the flow), it does not work:

```auto
msg.payload = Buffer.from(msg.payload)
return msg;

```

I have tried various variations , but did not find out why.  
What am i doing wrong?

Thanks

My flow:

```auto
[
    {
        "id": "7ef9dfab0cff67ba",
        "type": "tab",
        "label": "TCP REQUEST",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "cbcde23642237613",
        "type": "tcp request",
        "z": "7ef9dfab0cff67ba",
        "server": "192.168.20.40",
        "port": "50001",
        "out": "time",
        "ret": "buffer",
        "splitc": "1000",
        "name": "",
        "x": 510,
        "y": 200,
        "wires": [
            [
                "c4e9b6d3e5e0ad19",
                "6026b84bc8dc2e79"
            ]
        ]
    },
    {
        "id": "647d2e0b14640b05",
        "type": "debug",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 240,
        "wires": []
    },
    {
        "id": "522e649cb030ad01",
        "type": "function",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "func": "msg.payload = Buffer.from(msg.payload)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 260,
        "y": 240,
        "wires": [
            [
                "cbcde23642237613"
            ]
        ]
    },
    {
        "id": "c4e9b6d3e5e0ad19",
        "type": "function",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "func": "msg.payload = msg.payload.toString().split('.');\nmsg.payload[0];\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 240,
        "wires": [
            [
                "647d2e0b14640b05"
            ]
        ]
    },
    {
        "id": "baf28b66599c876c",
        "type": "inject",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "-p.?\\r",
        "payloadType": "str",
        "x": 80,
        "y": 240,
        "wires": [
            [
                "522e649cb030ad01"
            ]
        ]
    },
    {
        "id": "6026b84bc8dc2e79",
        "type": "debug",
        "z": "7ef9dfab0cff67ba",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 140,
        "wires": []
    }
]

```

---

<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 December 2021 15:57 UTC](https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676/2 "2021-12-03T15:57:01Z")

</div>

> [@peterda](#):
>
> does not work

Can you be more specific? Do you get an error or the wrong data in the buffer? Something else?

> [@peterda](#):
>
> `-p.?\\r`

The inject node string doesn't need `\r` to be escaped. The inject sends exactly what is entered. However, If you actually need a carriage return then you would need to append that using a function or use JSONata in the inject.

---

<div class="post-metadata">

**Author:** ![peterda](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@peterda](https://discourse.nodered.org/u/peterda)\
**Post date:** [3 December 2021 16:01 UTC](https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676/3 "2021-12-03T16:01:11Z")

</div>

> [@Steve-Mcl](#):
>
> If you actually need a carriage return then you would need to append that using a function or use JSONata in the inject.

I need the \r, i will try your suggestion!

Thank you very much for the tip!  
appreciate it.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 December 2021 16:06 UTC](https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676/4 "2021-12-03T16:06:05Z")

</div>

The inject node string input does not allow the use of \n \r \t etc it takes strings literally  
try using JSON or JSONata or a function node as in these examples

```auto
[{"id":"baf28b66599c876c","type":"inject","z":"7ef9dfab0cff67ba","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-p.?\\r","payloadType":"str","x":80,"y":240,"wires":[["522e649cb030ad01"]]},{"id":"522e649cb030ad01","type":"function","z":"7ef9dfab0cff67ba","name":"","func":"msg.payload = Buffer.from(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":240,"wires":[["6026b84bc8dc2e79"]]},{"id":"6026b84bc8dc2e79","type":"debug","z":"7ef9dfab0cff67ba","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":180,"wires":[]},{"id":"4e31b19e.e0cd38","type":"function","z":"7ef9dfab0cff67ba","name":"","func":"msg.payload = \"-p.?\\r\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":300,"wires":[["522e649cb030ad01"]]},{"id":"cf4039b4.5c2268","type":"inject","z":"7ef9dfab0cff67ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"-p.?\\r\"","payloadType":"json","x":90,"y":360,"wires":[["522e649cb030ad01"]]},{"id":"6ac4a5f9.547c34","type":"inject","z":"7ef9dfab0cff67ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"-p.?\\r\"","payloadType":"jsonata","x":90,"y":420,"wires":[["522e649cb030ad01"]]},{"id":"9416954b.60f8b","type":"inject","z":"7ef9dfab0cff67ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":300,"wires":[["4e31b19e.e0cd38"]]}]

```

---

<div class="post-metadata">

**Author:** ![peterda](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@peterda](https://discourse.nodered.org/u/peterda)\
**Post date:** [5 December 2021 11:12 UTC](https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676/5 "2021-12-05T11:12:11Z")

</div>

Thank you very much for your hints to a solution.  
It was not obvious that the special characters are stripped way.

Now i have a working proof of concept and can work on flows that will be implemented in production.

@E1cid sorry that i did not choose your solution, @Steve-Mcl was first at the finish line.

Cheers, Peter

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 December 2021 11:17 UTC](https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676/6 "2021-12-05T11:17:44Z")

</div>

Not an issue. I only added my response as Steve had omitted the use of JSON. Using JSON has less overhead than JSONata or a function node.

---

<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:** [19 December 2021 11:18 UTC](https://discourse.nodered.org/t/injecting-a-string-in-a-function-not-working/54676/7 "2021-12-19T11:18:37Z")

</div>

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