Hello friend i am new in node js, how we can get variable value used in mysql query anonymous function ?
var alldata = function(){
var http = require('http'), mysql = require('mysql');
var client = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: ''
});
client.connect();
client.query("use cakephp2");
client.query("SELECT id, title,body,created from posts",
function(err, results, fields) {
if (err) throw err;
var output = '<h1>Latest Posts</h1><ul><table border=1><tr>';
for (var index in fields) {
output += '<td>' + fields[index].name + '</td>';
}
output += '</tr>';
for (var index in results) {
output += '<tr><td>' + results[index].id + '</td>';
output += '<td>' + results[index].title + '</td>';
output += '<td>' + results[index].body + '</td>';
output += '<td>' + results[index].created + '</td></tr>';
}
output += '</ul>';
// console.log(output);
// return output;
}
);
return output ;
}
exports.alldatas = alldata();
in above code i did not found return output result while in client.query when use console.log(output) give correct result, but can not access output value outside of anonymous function.
please help me
Thanks in advance .