Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

pool de conexão node.js e mysql não exporta


Você está exportando uma função, não o próprio pool. Além disso, getConnection não está aceitando nenhum retorno de chamada:

db.js deve ser algo assim:
var mySQL = require('mysql');
var pool  = mySQL.createPool({
    host: config.host,
    user: config.user,
    password: config.password,
    database: config.database
});
var getConnection = function (cb) {
    pool.getConnection(function (err, connection) {
        //if(err) throw err;
        //pass the error to the cb instead of throwing it
        if(err) {
          return cb(err);
        }
        cb(null, connection);
    });
};
module.exports = getConnection;

query.js deve ser algo assim:
var getConnection = require('./db');
getConnection(function (err, con) {
  if(err) { /* handle your error here */ }
  var userQuery = 'select * from user';
  console.log("con: " + con); //displays undefined
  con.query(userQuery,function(err,user){
  con.release();
});