In my node application i am using function.But i am getting the result of that function:
My code:
var connection = mysql.createConnection({
host : 'localhost',
user : 'xxxx',
password : 'xxxxx',
database : 'xxxxxxx',
debug : true,
})
connection.connect(function(err) {
if ( !err ) {
console.log("Connected to MySQL");
} else if ( err ) {
console.log(err);
}
});
if(level5 == undefined)
{
var result1=querylevel5();
console.log("vvvvvvvvv="+result1)
res.writeHead(200, { 'Content-Type': 'application/json'});
res.end(JSON.stringify(result1,null,"\n"));
}
My function:
function querylevel5()
{
var result;
connection.query("select * from levels ", function(err, row1, fields) {
result= row1;
/*res.writeHead(200, { 'Content-Type': 'application/json'});
res.end(JSON.stringify(row1,null,"\n"));*/
});
return result;
}
I want to get the row1 result ,in my calling function..But its printing "undefined"..I am new to this node.js..So please help me to solve this..Thanks in advance..
connectionis.connection.queryis part of e.g.node-mysqlor another sql module. So most likely it is an async call, so the callback function is not called at the time where you do thereturn result;Joachim Pileborg