Anda mengekspor fungsi, bukan kumpulan itu sendiri. Selain itu, getConnection tidak menerima panggilan balik apa pun:
db.js seharusnya seperti ini:
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 seharusnya seperti ini:
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();
});