# "TypeError: Cannot read property 'split' of undefined"

**URL:** <https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195>\
**Category:** General\
**Created:** [27 October 2019 13:52 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195 "2019-10-27T13:52:43Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![injector22](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@injector22](https://discourse.nodered.org/u/injector22)\
**Post date:** [27 October 2019 13:52 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/1 "2019-10-27T13:52:44Z")

</div>

I can't seem to figure out why I'm getting this error when trying to use the split method in a function node.

"TypeError: Cannot read property 'split' of undefined"

Here's my function code:

```auto
msg.payload = (msg._topic.split("/"))[1];
return msg;

```

and my debug output

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0851fc0807e4a4acd126889a57cd5b4e64384fd0.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [27 October 2019 15:40 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/2 "2019-10-27T15:40:12Z")

</div>

Use the debug tooltips ([https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)) to make sure your underline is the right character

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 October 2019 16:04 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/3 "2019-10-27T16:04:10Z")

</div>

most msg don't have an \_topic property, so if one of those arrived at your function it would be undefined and so split would not work. \_topic is added by an incoming MQtt node - so as long as all your msg are coming from there and nowhere else you may be ok.

---

<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:** [27 October 2019 16:18 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/4 "2019-10-27T16:18:37Z")

</div>

Do a simple check first...

```auto
if(!msg._topic){
  //_topic is empty. 
  //Consider calling node.warn() here
   return null;// halt flow 
}
msg.payload = (msg._topic.split("/"))[1];
return msg;

```

---

<div class="post-metadata">

**Author:** ![injector22](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@injector22](https://discourse.nodered.org/u/injector22)\
**Post date:** [27 October 2019 17:14 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/5 "2019-10-27T17:14:20Z")

</div>

> Use the debug tooltip messages to make sure your underline is the right character

\_topic is correct, If I do `msg.payload = msg._topic` and payload turns into \_topic.

> most msg don't have an \_topic property, so if one of those arrived at your function it would be undefined and so split would not work. \_topic is added by an incoming MQtt node - so as long as all your msg are coming from there and nowhere else you may be ok.

Yep, it's coming directly from the MQTT telemetry node.

> Do a simple check first...

In a normal case I'd agree but the function node is behind a switch node which filters out only the statuses I want and drops everything else.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/41db93022fdfcfd9987c03a08fd865cb3cec9258.png)

---

<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:** [27 October 2019 17:26 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/6 "2019-10-27T17:26:18Z")

</div>

Put a `node.warn(msg._topic)` in before the split and check it shows ok. Also completely retype the split line to make sure there are no hidden characters, then copy/paste that and edit it to make the node.warn line.

---

<div class="post-metadata">

**Author:** ![injector22](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@injector22](https://discourse.nodered.org/u/injector22)\
**Post date:** [28 October 2019 21:05 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/7 "2019-10-28T21:05:15Z")

</div>

> [@Colin](#):
>
> node.warn(msg.\_topic)

That showed the \_topic as expected

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/769261782c17caeec02aded61baa036034d402ad.png)

---

<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:** [28 October 2019 21:30 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/8 "2019-10-28T21:30:59Z")

</div>

And what happened next?

---

<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:** [28 October 2019 21:39 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/9 "2019-10-28T21:39:06Z")

</div>

Meanwhile I am trying to replicate this. I don't see \_topic coming from mqtt nodes. What is this \_topic?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 October 2019 22:02 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/10 "2019-10-28T22:02:29Z")

</div>

@Colin - it's only added to msgs coming from localhost. (It's a vague attempt to try help avoid local loops if you try to publish back to the same topic. )

---

<div class="post-metadata">

**Author:** ![injector22](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@injector22](https://discourse.nodered.org/u/injector22)\
**Post date:** [28 October 2019 22:03 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/11 "2019-10-28T22:03:51Z")

</div>

@Colin

Here's the code for the section of the flow.

```auto
[{"id":"a703dc55.9112e","type":"subflow","name":"Detect online","info":"","in":[{"x":260,"y":100,"wires":[{"id":"c43e8062.fcccd"},{"id":"19c1055b.534ddb"},{"id":"3b9748cc.9d37d8"}]}],"out":[{"x":1440,"y":140,"wires":[{"id":"c4aca534.c6b218","port":0},{"id":"f55f171a.d79838","port":0}]}]},{"id":"41a8cf72.9d81e","type":"delay","z":"a703dc55.9112e","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":870,"y":160,"wires":[["9ad7a565.51bf18"]]},{"id":"f55f171a.d79838","type":"inject","z":"a703dc55.9112e","name":"Startup","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"x":930,"y":260,"wires":[["9ad7a565.51bf18"]]},{"id":"19c1055b.534ddb","type":"change","z":"a703dc55.9112e","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":160,"wires":[["41a8cf72.9d81e"]]},{"id":"ec9f7a00.159c98","type":"delay","z":"a703dc55.9112e","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":660,"y":200,"wires":[["41a8cf72.9d81e"]]},{"id":"c43e8062.fcccd","type":"change","z":"a703dc55.9112e","name":"Online","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":110,"wires":[["ff391aea.68c608"]]},{"id":"9ad7a565.51bf18","type":"change","z":"a703dc55.9112e","name":"Offline","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":160,"wires":[["ff391aea.68c608"]]},{"id":"c4aca534.c6b218","type":"rbe","z":"a703dc55.9112e","name":"","func":"rbe","gap":"","start":"","inout":"out","x":1300,"y":140,"wires":[[]]},{"id":"3b9748cc.9d37d8","type":"change","z":"a703dc55.9112e","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"timeout","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":200,"wires":[["ec9f7a00.159c98"]]},{"id":"b19de89d.9b57f8","type":"comment","z":"a703dc55.9112e","name":"Each incoming message will retrigger a new timeout (msg.timeout [ms]) before being reported offline","info":"","x":590,"y":40,"wires":[]},{"id":"ff391aea.68c608","type":"change","z":"a703dc55.9112e","name":"Offline","rules":[{"t":"set","p":"topic","pt":"msg","to":"isonline","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1170,"y":140,"wires":[["c4aca534.c6b218"]]},{"id":"8ed7b581.57e978","type":"mqtt in","z":"de212f36.d340b","name":"","topic":"stat/GridController/#","qos":"1","datatype":"auto","broker":"97bbbbb4.7644a8","x":690,"y":500,"wires":[["bef9519e.7092f"]]},{"id":"bef9519e.7092f","type":"subflow:a703dc55.9112e","z":"de212f36.d340b","name":"","env":[],"x":810,"y":540,"wires":[["a6f5392e.1094d8","e5e93740.caa458"]]},{"id":"a6f5392e.1094d8","type":"ui_switch","z":"de212f36.d340b","name":"","label":"{{msg.topic}}","tooltip":"","group":"619958a1.f70658","order":1,"width":"0","height":"0","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"check_circle","oncolor":"green","offvalue":"false","offvalueType":"bool","officon":"error_outline","offcolor":"red","x":1010,"y":460,"wires":[[]]},{"id":"e5e93740.caa458","type":"switch","z":"de212f36.d340b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":true,"outputs":1,"x":990,"y":560,"wires":[["a807cfb5.e4719","4028ebb8.e15c34"]]},{"id":"a807cfb5.e4719","type":"debug","z":"de212f36.d340b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1210,"y":440,"wires":[]},{"id":"58824858.6b8228","type":"debug","z":"de212f36.d340b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1310,"y":540,"wires":[]},{"id":"4028ebb8.e15c34","type":"function","z":"de212f36.d340b","name":"","func":"node.warn(msg._topic);\nmsg.payload = (msg._topic.split(\"/\"))[1];\nreturn msg;","outputs":1,"noerr":0,"x":1150,"y":560,"wires":[["58824858.6b8228"]]},{"id":"97bbbbb4.7644a8","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"619958a1.f70658","type":"ui_group","z":"","name":"Status","tab":"83c3bfd.350b84","disp":true,"width":"6","collapse":false},{"id":"83c3bfd.350b84","type":"ui_tab","z":"","name":"Status","icon":"dashboard","order":4,"disabled":false,"hidden":false}]

```

---

<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 October 2019 16:58 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/12 "2019-10-29T16:58:34Z")

</div>

I have imported your flow and connected it to an appopriate mqtt in node and it works as expected for me.  
What hardware and operating system are you using?  
Restart node red and paste the log you get here please. That is the log containing the Welcome to node red message that you get when you start node-red in a terminal. Copy/paste the text if possible please rather than posting a screenshot.

---

<div class="post-metadata">

**Author:** ![Picman1](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Picman1](https://discourse.nodered.org/u/Picman1)\
**Post date:** [30 April 2020 09:49 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/13 "2020-04-30T09:49:30Z")

</div>

My code worked up until the 26th.  
It is the JavaScript that does not like the split method.

Error:  
TypeError: Cannot read property 'split' of undefined

---

<div class="post-metadata">

**Author:** ![Picman1](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Picman1](https://discourse.nodered.org/u/Picman1)\
**Post date:** [30 April 2020 10:02 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/14 "2020-04-30T10:02:21Z")

</div>

> [@Steve-Mcl](#):
>
> if(!msg.\_topic){ //\_topic is empty. //Consider calling node.warn() here return null;// halt flow } msg.payload = (msg.\_topic.split("/"))[1]; return msg;

It does not give an error, no result. 🤨

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 April 2020 10:03 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/15 "2020-04-30T10:03:58Z")

</div>

Check your input. Split works fine, the input you giving it is not.

---

<div class="post-metadata">

**Author:** ![Picman1](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Picman1](https://discourse.nodered.org/u/Picman1)\
**Post date:** [30 April 2020 10:13 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/16 "2020-04-30T10:13:03Z")

</div>

The vendor change it web site ☹

---

<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:** [30 April 2020 10:15 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-split-of-undefined/17195/17 "2020-04-30T10:15:09Z")

</div>

What @bakman2 says ⬆

try putting node.warn in your code to see whats going on e.g.

```auto
node.warn(["msg._topic",msg._topic]);
if(!msg._topic){
  node.warn("msg._topic is empty!"); 
  //_topic is empty. 
  //Consider calling node.warn() here
   return null;// halt flow 
}
node.warn("msg._topic is NOT empty - gonna split on '/' does it even have a '/' in it?"); 
msg.payload = (msg._topic.split("/"))[1];
node.warn("msg.payload is now " + msg.payload);
return msg;

```

... then look in the debug window

PS, are you sure you want `_topic`?
