Function tried to send a message of type number - But a number is what I want

So I have a function connected to a MySQL database selecting a certain column from a table, the colums are numbers, either 0 or 1. I know I can get the values, but now I want to send them into some toggle nodes that respond to either 0 or 1. The problem is that "Function tried to send a message of type number".

I know that an object has to be returned, not anything else, I just don't know how to get the value from that object, if that makes sense.

The flow is down below, any input would be appreciated.

[{"id":"14f759e5.245236","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"874b5466.7c71b8","type":"toggle","z":"14f759e5.245236","name":"","onOffTopic":"","onValue":"0","onType":"str","offValue":"1","offType":"str","toggleTopic":"","toggleValue":"","toggleType":"num","passOnOff":"","x":670,"y":720,"wires":[["7c6e4b1d.90a9c4"]]},{"id":"e3b1efb8.6395e","type":"toggle","z":"14f759e5.245236","name":"","onOffTopic":"","onValue":"0","onType":"str","offValue":"1","offType":"str","toggleTopic":"","toggleValue":"","toggleType":"num","passOnOff":"","x":830,"y":720,"wires":[["536bca97.11b894"]]},{"id":"9e7a4c95.56722","type":"toggle","z":"14f759e5.245236","name":"","onOffTopic":"","onValue":"0","onType":"str","offValue":"1","offType":"str","toggleTopic":"","toggleValue":"","toggleType":"num","passOnOff":"","x":990,"y":720,"wires":[["3901f62c.cfbe6a"]]},{"id":"a2c28216.6f883","type":"toggle","z":"14f759e5.245236","name":"","onOffTopic":"","onValue":"0","onType":"str","offValue":"1","offType":"str","toggleTopic":"","toggleValue":"","toggleType":"num","passOnOff":"","x":1150,"y":720,"wires":[["64371f8e.3448"]]},{"id":"fb0d45c3.2be958","type":"mysql","z":"14f759e5.245236","mydb":"7c9bf018.9a728","name":"DB","x":770,"y":220,"wires":[["ff86bc12.40229","6da739be.230458"]]},{"id":"4076ed6.a385314","type":"function","z":"14f759e5.245236","name":"","func":"msg.topic=\"select status from doors\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":220,"wires":[["fb0d45c3.2be958"]]},{"id":"6da739be.230458","type":"debug","z":"14f759e5.245236","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":220,"wires":[]},{"id":"d21ae6d7.8a9ce8","type":"inject","z":"14f759e5.245236","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":390,"y":220,"wires":[["4076ed6.a385314"]]},{"id":"ff86bc12.40229","type":"function","z":"14f759e5.245236","name":"","func":"var msg1 = msg.payload[0].status;\nvar msg2 = msg.payload[1].status;\nvar msg3 = msg.payload[2].status;\nvar msg4 = msg.payload[3].status;\n\nreturn [msg1, msg2, msg3, msg4];","outputs":4,"noerr":0,"initialize":"","finalize":"","x":880,"y":420,"wires":[["874b5466.7c71b8","59563b71.f45364"],["e3b1efb8.6395e","28065b01.8585e4"],["9e7a4c95.56722","fa89a3e4.78355"],["a2c28216.6f883","5429827e.ccce1c"]]},{"id":"59563b71.f45364","type":"debug","z":"14f759e5.245236","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1410,"y":260,"wires":[]},{"id":"28065b01.8585e4","type":"debug","z":"14f759e5.245236","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1410,"y":400,"wires":[]},{"id":"fa89a3e4.78355","type":"debug","z":"14f759e5.245236","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1410,"y":460,"wires":[]},{"id":"5429827e.ccce1c","type":"debug","z":"14f759e5.245236","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1390,"y":520,"wires":[]},{"id":"64371f8e.3448","type":"blynk-websockets-out-write","z":"14f759e5.245236","name":"Door4","pin":"3","client":"64c044e6.ea73dc","x":1150,"y":840,"wires":[]},{"id":"7c6e4b1d.90a9c4","type":"blynk-websockets-out-write","z":"14f759e5.245236","name":"Door1","pin":0,"client":"64c044e6.ea73dc","x":670,"y":840,"wires":[]},{"id":"536bca97.11b894","type":"blynk-websockets-out-write","z":"14f759e5.245236","name":"Door2","pin":"1","client":"64c044e6.ea73dc","x":830,"y":840,"wires":[]},{"id":"3901f62c.cfbe6a","type":"blynk-websockets-out-write","z":"14f759e5.245236","name":"Door3","pin":"2","client":"64c044e6.ea73dc","x":990,"y":840,"wires":[]},{"id":"7c9bf018.9a728","type":"MySQLdatabase","name":"","host":"localhost","port":"3306","db":"dbiot","tz":"","charset":"UTF8"},{"id":"64c044e6.ea73dc","type":"blynk-websockets-client","name":"Doors","path":"ws://cloud.blynk.cc:8080/websocket","key":"wkKLjya69gblD28h5uZELJw91h0CXJH-"}]

msg has to be an object so you have to use
msg1.payload = msg.payload[0].status

Then you access your number later in flow using msg.payload.

Does that work with multiple outputs? I tried it but it says ""ReferenceError: msg1 is not defined (line 1, col 1)""

If I define msg1, msg2 etc before i set their payload values to the Array value, nothing happens. I don't even get an output, from any of the 4 outputs.

let msg1={}, msg2={}, msg3={}, msg4={};
// they are now defined as objects
msg1.payload = msg.payload[0].status;
//etc
return [msg1,msg2,msg3,msg4];

Thanks a lot, that made it work. I completely missed that you can define them as objects like that.

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