Database, problem with require

hi!
I should read from a database (I used xaamp with a mysql database). I put this code in Function (node), but I'm having problems with 'require'. I followed the guide to add external modules, but I can't solve it. Could anyone show some examples of database reading?
FUNCTION:

var mysql = require('mysql');

var connection = mysql.createConnection( {  host : 'localhost',
  user : 'nome_utente',
  password : 'password_utente',
  database : 'nome_db'
} );
connection.connect();

var queryString = 'SELECT cmp FROM tbl ORDER By id DESC';

connection.query(queryString, function(err, rows, fields) {
  if (err) throw err;
  for (var i in rows) {
      console.log('Valori: ', rows[i].cmp);
  }
});

connection.end();

I use MySQL. I use the MySQL node. https://flows.nodered.org/node/node-red-node-mysql
It is very easy. No 'require' at all.
msg.topic has the select statement and it just works.

1 Like

Thanks, your solution is certainly more efficient!

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