TypeError: Cannot read property 'text' of undefined

I am trying to write a function to examine the status of a ui_switch and an http request to control the switch depending if the msg.payload of the http request exists.
I am getting a function error. I have narrowed it down to something related to the 'cli' variable.

When I look at the debug I see msg.payload with data. Here is the flow:

[{"id":"715afb2c.159e94","type":"http request","z":"e5f42280.90e5b","name":"GetClient","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":460,"y":260,"wires":[["c8e3d7b9.a16d48","8f2b0a81.6183d8"]]},{"id":"861598b8.a6e938","type":"status","z":"e5f42280.90e5b","name":"","scope":["d07f6cdd.fdc22"],"x":560,"y":320,"wires":[["8f2b0a81.6183d8"]]},{"id":"8f2b0a81.6183d8","type":"function","z":"e5f42280.90e5b","name":"","func":"var cli = flow.get(\"client\");\nvar sw = msg.status.text;\nif ((sw===undefined) && (cli===undefined)) //no client, radio off\n{\n       return null;\n}\nif ((sw==='on') && (cli===undefined)) //no client, radio on\n{\n    var swcontrol = {payload: 0}\n}\nif ((sw==='on') && (cli!==undefined)) // client on , radio on\n{\n    return null; \n}\n\n// node.send({\"payload\":msg.status.text});\nreturn swcontrol;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":740,"y":300,"wires":[["47cc81da.1660a"]]},{"id":"d07f6cdd.fdc22","type":"ui_switch","z":"e5f42280.90e5b","name":"","label":"<font size = 5> Flex 6400","tooltip":"","group":"e357ef02.ef3cb","order":1,"width":"6","height":"1","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"fa-power-off fa-2x","oncolor":"lime","offvalue":"0","offvalueType":"num","officon":"fa-power-off fa-2x","offcolor":"gray","x":310,"y":360,"wires":[["d8545ad.2bbc9a8"]]},{"id":"d8545ad.2bbc9a8","type":"rpi-gpio out","z":"e5f42280.90e5b","name":"","pin":"7","set":true,"level":"0","freq":"","out":"out","x":740,"y":360,"wires":[]},{"id":"3ed530a6.cf73","type":"change","z":"e5f42280.90e5b","name":"getclientUrl","rules":[{"t":"set","p":"url","pt":"msg","to":"$globalContext(\"frstackurl\") & \"/Radio/CLIENT\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":280,"wires":[["715afb2c.159e94"]]},{"id":"434e0e28.56182","type":"inject","z":"e5f42280.90e5b","name":"3secs","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3","crontab":"","once":true,"onceDelay":"5","topic":"","payload":"","payloadType":"date","x":80,"y":140,"wires":[["b313b612.ed68a8","b8326128.8410d","3ed530a6.cf73"]]},{"id":"e357ef02.ef3cb","type":"ui_group","z":"","name":"Radios","tab":"4bc17c6e.b74934","order":1,"disp":true,"width":"6","collapse":true},{"id":"4bc17c6e.b74934","type":"ui_tab","z":"","name":"Station","icon":"dashboard","order":6,"disabled":false,"hidden":false}]

Here is the function I have written. I am sure the problem will be something trivial. Thanks for the help on this...

var cli = flow.get("client");
var sw = msg.status.text;
if ((sw===undefined) && (cli===undefined)) //no client, radio off
{
       return null;
}
if ((sw==='on') && (cli===undefined)) //no client, radio on
{
    var swcontrol = {payload: 0}
}
if ((sw==='on') && (cli!==undefined)) // client on , radio on
{
    return null; 
}

// node.send({"payload":msg.status.text});
return swcontrol;

I suspect you are trying to "get" the "on" or "off" state of the switch - that is not really how you do it.

just wire the switch direct to the function & inspect the value of msg.payload.

If you need to store the value of the switch for later use, then store its value flow of global in context.

Ps, use debug nodes to show you what is coming out of each node.

If all of this sounds a bit new to new, then it all in the excellent docs and in particular, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

I haven't tried your flow, but looking at the code, it looks as though swcontrol is being used out of scope.

If you look at your conditional statements, swcontrol is only defined if the statement;
if ((sw==='on') && (cli===undefined)) is true.

Otherwise return swcontrol; is trying to return a value that is not defined.

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