I've got two node.js files: server.js and database.js. I want my socket.io emitting to happen in server.js and the database queries in database.js. Server.js:
// init
io.sockets.on('connection', function(socket) {
initdb = db.initdb();
console.log(initdb)
});
My database.js contains basically the following code:
function query(queryString) {
connection = mysql.createConnection({
host: '12.23.45.67',
user: 'user',
password: 'password',
database: 'database'
});
connection.connect();
var res = connection.query(queryString, function(err, rows, fields) {
if (err) throw err;
});
connection.end();
}
// export initdb for external usage by server.js
exports.initdb = function() {
var initdb = query("SELECT * FROM columns;");
};
My problem is that I want the rows object from within the connection.query function to be returned to my initdb function. However the only place where I can log this object is within that function. How can I pass the query results so I can emit the JSON object it from server.js?