Hello and TIA for anyone that could lead me to a solution..
I'm trying to make a SELECT query to a mysql database using mysql2 but the result when calling the function is always a promisse.
I do manage to get data from the database but only inside the function...
Any help is appreciated. Thanks!
I've set node-red settinhs.js as:
functionGlobalContext: {
os:require('os'),
fs:require('fs'),
mysql2:require('mysql2'),
// jfive:require("johnny-five"),
// j5board:require("johnny-five").Board({repl:false})
},
// Allow the Function node to load additional npm modules
functionExternalModules: true,
I do have the console showing data:
Flow:
[{"id":"e31e66e2.5af808","type":"function","z":"c4f601fe.4aba7","name":"getDevices","func":"\ncontext.global.getDevices = async function getData(){\n \n //Function checkin\n node.warn(\"DEBUG 1\")\n \n const mysql = global.get(\"mysql2\")\n \n //create the pool\n const connection = await mysql.createConnection({host:'--YOURSERVER--', user: '--YOURUSERNAME--', password:'--YOURPASS--', database: 'mika'});\n const [rows, fields] = await connection.promise().execute('SELECT 1');\n \n\n node.warn(\"DEBUG 2\" + JSON.stringify(rows))\n //Data is printed in console log.\n \n return rows\n \n}\n\n//Calls function and display the results\nnode.warn(\"RESULT is \" + context.global.getDevices());\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"mysql","module":"mysql2"}],"x":1030,"y":240,"wires":[[]]},{"id":"77d1c752.3dabd8","type":"inject","z":"c4f601fe.4aba7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"mysql","payload":"","payloadType":"date","x":1040,"y":160,"wires":[["e31e66e2.5af808"]]}]