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

#1

I am trying to learn this, so was reading the info on this page: https://nodered.org/docs/writing-functions.
Mainly because I would like to split this:
split%20this%2001
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?

0 Likes

#2

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.

0 Likes

#3

Sorry. Hope it's good now.

0 Likes

#4

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
0 Likes

#5

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

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

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

0 Likes

#6

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.

0 Likes

#7

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
Thanks for the advice

0 Likes

#8

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

1 Like