I'm learning Node.js and I'm just starting to work with some MySQL connections. I have a function which is supposed to get a set of rows from the database. However I can't get the value from it.
//connect.js
var mysql = require('mysql');
module.exports = {
world : function () {
var conn = mysql.createConnection({
host : '',
user : 'root',
password : '',
database : ''
});
conn.connect(function(err){
if(err) throw err;
});
conn.query('SELECT * FROM `room` WHERE 1', function(err, result, fields){
if(err) throw err;
var id = JSON.stringify(result[0].id);
var name = JSON.stringify(result[0].name);
var time = JSON.stringify(result[0].time);
return time;
});
conn.end(function(err){
if(err) throw err;
})
}
};
And I try to get the value by this program:
//show.js
var hello = require('./connect');
console.log(hello.world());
The result like this:
$ node show
undefined
So how should I get the value?