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

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:

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

and my debug output

image

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

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.

Do a simple check first...

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

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.

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.

That showed the _topic as expected

image

And what happened next?

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

@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. )

@Colin

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

[{"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}]

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.

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

It does not give an error, no result. :face_with_raised_eyebrow:

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

The vendor change it web site :frowning:

What @bakman2 says :arrow_up:

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

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?