Populate dropdown with Database Information

I would like to share with you here an information that I struggled a lot to learn.

And I myself may end up needing to use this in the future.

If you have a table with two fields:

Code and Name

Most likely you will want to put the primary key in the combo value and the name in the label.

However, I was very doubtful about how to manipulate the array to do this considering that the database node returns with the name of the field in the array and this interferes with popularizing the dropdown.

Below is how I solved the problem, after much research here on the forum.

image

$$.payload.$merge([{$.Descompressao: $.nDescompressao}])

Follow the complete code:

[{"id":"4639c7fae2510b5a","type":"subflow","name":"PrepBD","info":"","category":"","in":[{"x":80,"y":100,"wires":[{"id":"8cc0b55b7d0743c0"}]}],"out":[{"x":1020,"y":100,"wires":[{"id":"4135b832b386a610","port":0}]}],"env":[],"meta":{},"color":"#DDAA99","status":{"x":1020,"y":200,"wires":[{"id":"84933fc7168ae2b4","port":0}]}},{"id":"8cc0b55b7d0743c0","type":"change","z":"4639c7fae2510b5a","name":"copy flow/global to msg","rules":[{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.ip","pt":"msg","to":"#:(persistent)::IP_vGlobalBD","tot":"global"},{"t":"set","p":"payload.porta","pt":"msg","to":"#:(persistent)::Porta_vGlobalBD","tot":"global"},{"t":"set","p":"payload.usuario","pt":"msg","to":"#:(persistent)::Usuario_vGlobalBD","tot":"global"},{"t":"set","p":"payload.senha","pt":"msg","to":"#:(persistent)::Senha_vGlobalBD","tot":"global"},{"t":"set","p":"payload.nomeBD","pt":"msg","to":"#:(persistent)::NomeBD_vGlobalBD","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":100,"wires":[["effa65099dd2a525"]]},{"id":"4135b832b386a610","type":"mysql2","z":"4639c7fae2510b5a","name":"","server":"","bind":"","topic":"","x":760,"y":100,"wires":[[]]},{"id":"effa65099dd2a525","type":"function","z":"4639c7fae2510b5a","name":"prep_MQTT_Conn","func":"\nconst ip = msg.payload.ip\nconst porta = msg.payload.porta\nconst usuario = msg.payload.usuario\nconst senha = msg.payload.senha\nconst nomeBD = msg.payload.nomeBD\n\nmsg.server = {\n    \"host\": ip,\n    \"port\": porta,\n    \"username\": usuario,\n    \"password\": senha,\n    \"db\": nomeBD\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":100,"wires":[["4135b832b386a610"]]},{"id":"eb96b8b79efaea17","type":"change","z":"4639c7fae2510b5a","name":"","rules":[{"t":"set","p":"server","pt":"msg","to":"{'host': $globalContext('IP_vGlobalBD'),\t 'port': $globalContext('Porta_vGlobalBD'),\t 'username': $globalContext('Usuario_vGlobalBD'),\t 'password': $globalContext('Senha_vGlobalBD'),\t 'bd': \"TelemetriaEnervision\"\t }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":260,"wires":[[]]},{"id":"4a50ea77c5d0ed5b","type":"comment","z":"4639c7fae2510b5a","name":"Quem sabe futuro usar esse Change","info":"Não consegui fazer funcionar com o mysql2, ele diz que o nome do banco de dados não está selecionado.","x":320,"y":220,"wires":[]},{"id":"84933fc7168ae2b4","type":"status","z":"4639c7fae2510b5a","name":"","scope":null,"x":840,"y":200,"wires":[[]]},{"id":"bf3f1bc774f79ad2","type":"inject","z":"702cd2e8c3c094c0","name":"4s","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"4","topic":"","payload":"","payloadType":"date","x":210,"y":340,"wires":[["c08a461e52c2d290"]]},{"id":"c08a461e52c2d290","type":"function","z":"702cd2e8c3c094c0","name":"fSQL","func":"msg.topic = \"SELECT Descompressao, nDescompressao FROM Descompressao;\"\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":340,"wires":[["fedeb6c6f7cd8a31"]]},{"id":"fedeb6c6f7cd8a31","type":"subflow:4639c7fae2510b5a","z":"702cd2e8c3c094c0","name":"","x":560,"y":340,"wires":[["e2662c3423228f4d"]]},{"id":"e2662c3423228f4d","type":"change","z":"702cd2e8c3c094c0","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"$$.payload.$merge([{$.Descompressao: $.nDescompressao}])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":340,"wires":[["ef7ca0224b5fc51e"]]},{"id":"ef7ca0224b5fc51e","type":"ui_dropdown","z":"702cd2e8c3c094c0","name":"","label":"DROP TESTE","tooltip":"","place":"Select option","group":"ad8cd5278474e0cd","order":2,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","className":"","x":1000,"y":340,"wires":[["061302a1614464f1"]]},{"id":"061302a1614464f1","type":"debug","z":"702cd2e8c3c094c0","name":"debug 20","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1200,"y":340,"wires":[]},{"id":"ad8cd5278474e0cd","type":"ui_group","name":"Default","tab":"c377269dc140e28f","order":1,"disp":true,"width":"6","collapse":false},{"id":"c377269dc140e28f","type":"ui_tab","name":"Home","icon":"dashboard","order":6,"disabled":false,"hidden":false}]

You have not shown the db/subflow output format, so I am not 100% on this,.
I have a feeling the merge is not required.
This could also possibly work

$$.payload.{$.Descompressao: $.nDescompressao}[]

I beg your pardon, I really forgot to post the subflow.

I edited the post and included the subflow.

these two fields.

Examples are:

Decompression = Choice 01

nDecompression = value

1 Like