# "TypeError: msg.payload.split is not a function"

**URL:** <https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656>\
**Category:** General\
**Created:** [7 November 2018 18:38 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656 "2018-11-07T18:38:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AD3D](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ad3d/32/2731_2.png) [@AD3D](https://discourse.nodered.org/u/AD3D)\
**Post date:** [7 November 2018 18:38 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656/1 "2018-11-07T18:38:20Z")

</div>

I am trying to learn this, so was reading the info on this page: [Redirecting…](https://nodered.org/docs/writing-functions).  
Mainly because I would like to split this:  
 ![split%20this%2001](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5f5ccd780e92a7d838ada1d548da7874b11463cb.jpeg)  
Output is coming from a html node as a single message containing an array in msg.payload.  
(in case off is this a part of the flow)

> `[{"id":"d2b743ab.0d34f","type":"debug","z":"1cab3146.72bbcf","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":480,"wires":[]},{"id":"6e5c1a6c.76609c","type":"function","z":"1cab3146.72bbcf","name":"get values","func":"var outputMsgs = [];\nvar words = msg.payload.split(\" \");\nfor (var w in words) {\n outputMsgs.push({payload:words[w]});\n}\nreturn [outputMsgs];","outputs":2,"noerr":0,"x":650,"y":480,"wires":[["d2b743ab.0d34f"],[]]},{"id":"37d730e1.bfdcb8","type":"http request","z":"1cab3146.72bbcf","name":"get info","method":"GET","ret":"txt","url":"http://192.168.2.33","tls":"","x":280,"y":520,"wires":[["a28f6034.7c2028"]]},{"id":"9870e91c.b1968","type":"inject","z":"1cab3146.72bbcf","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":520,"wires":[["37d730e1.bfdcb8"]]},{"id":"b12fde4e.57f228","type":"debug","z":"1cab3146.72bbcf","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":560,"wires":[]},{"id":"a28f6034.7c2028","type":"html","z":"1cab3146.72bbcf","name":"","property":"payload","outproperty":"payload","tag":"h3","ret":"html","as":"single","x":430,"y":520,"wires":[["b12fde4e.57f228","6e5c1a6c.76609c"]]}]`

This only produces a error.  
"TypeError: msg.payload.split is not a function"  
Where am I going wrong with this?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 November 2018 18:58 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656/2 "2018-11-07T18:58:39Z")

</div>

Please read this post in sharing code on the forum and then edit your post to fix up the formatting. The XML is being treated as HTML and not displaying.

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![AD3D](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ad3d/32/2731_2.png) [@AD3D](https://discourse.nodered.org/u/AD3D)\
**Post date:** [7 November 2018 19:25 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656/3 "2018-11-07T19:25:38Z")

</div>

Sorry. Hope it's good now.

---

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [7 November 2018 19:57 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656/4 "2018-11-07T19:57:27Z")

</div>

Can you add the HTML that the "Get Info" http call is returning? It's hard to know for sure what's wrong without knowing what is in the message.

Some notes:

- Based on your screen shot, it looks like msg.payload contains an array not a string... split is not a method on arrays (it's on strings), so that might be the issue
- If you want to split an array into multiple messages, it would be easier to just use the "split" node as that is it's default behavior

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 November 2018 20:59 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656/5 "2018-11-07T20:59:55Z")

</div>

@dustinw Dustin is correct, the payload is an array. If youi want to split one of the members of the array you could use

```auto
var words = msg.payload[0].split(" ");

```

But maybe the question you need to answer is "What are you trying to do?"

---

<div class="post-metadata">

**Author:** ![AD3D](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ad3d/32/2731_2.png) [@AD3D](https://discourse.nodered.org/u/AD3D)\
**Post date:** [7 November 2018 22:55 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656/6 "2018-11-07T22:55:28Z")

</div>

Thanks for the response.  
Adding the code from the HTML is not that easy, I think. It's generated by a device (Esp8266 with two relay output) that is standing here.  
But as I understand it now: split is not a method on arrays (it's on strings), so that is the issue.  
Didn't know that for sure because I have a flow where I do almost the same thing but where I receive some values.  
But, like you said, in this case I have to look at the split node probably.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![AD3D](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ad3d/32/2731_2.png) [@AD3D](https://discourse.nodered.org/u/AD3D)\
**Post date:** [7 November 2018 23:59 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656/7 "2018-11-07T23:59:10Z")

</div>

> [@zenofmud](#):
>
> "What are you trying to do?"

Good question.  
I would like to split this array in two numbers (5 and 4 in this case) and there state (5 = off and 4 = off in this case) would be the very short story.  
The long story is probably very boring because I am just trying to find out the best/easiest way to make a web device that is not depending on any service/api/cloud than the highly necessary ones. It's basically just a device with two buttons and two outputs. It also has two buttons on a webpage that are related to the two buttons/outputs on the device. Pushing a button (on the device or the webpage) will change the state, depending of the current state.  
It would be nice, better say, necessary to see in what state an output is. So I think I would need two messages in this case.  
I tryed your advice and so far, so good  
 ![split%20this%2002](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4511a735c591a72fe318b0acef2e21cd0c4e8621.jpeg)  
Thanks for the advice

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [8 November 2018 00:30 UTC](https://discourse.nodered.org/t/typeerror-msg-payload-split-is-not-a-function/4656/8 "2018-11-08T00:30:34Z")

</div>

Great, now you just need to process those arrays and you will be on your way 😀
