# TypeError: Cannot read property 'text' of undefined

**URL:** https://discourse.nodered.org/t/typeerror-cannot-read-property-text-of-undefined/35928
**Category:** General
**Created:** [14 November 2020 20:38 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-text-of-undefined/35928 "2020-11-14T20:38:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mick/32/80990_2.png) [@Mick](https://discourse.nodered.org/u/Mick)
#### Post date: [14 November 2020 20:38 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-text-of-undefined/35928/1 "2020-11-14T20:38:52Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/8816d38ea6d49405d33098d6a926d1118e3242f0.png)

```auto
[{"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...

```auto
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;

```

---

<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: [14 November 2020 20:48 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-text-of-undefined/35928/2 "2020-11-14T20:48:55Z")

</div>

> [@Mick](#):
>
> I am trying to write a function to examine the status of a ui\_switch

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](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [14 November 2020 20:50 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-text-of-undefined/35928/3 "2020-11-14T20:50:31Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [13 January 2021 20:50 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-text-of-undefined/35928/4 "2021-01-13T20:50:58Z")

</div>

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