2

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 .

Bineet Chaubey
  • 527
  • 3
  • 13

2 Answers2

2

You won't be able to access that variable outside the callback function. The reason is, the Node.js has a special feature of passing a callback function as the next block of code to be executed after performing an asynchronous IO task, (in your case a mysql query).

The code you write after the callback function gets executed immediately when your program goes into IO mode. And the output variable is not ready untill the callback is fired. and hence you can not access it.

You can read here

You will have to ue the output within that callback function or call some other function there and pass output to it as a parameter.

Community
  • 1
  • 1
Salman
  • 9,299
  • 6
  • 40
  • 73
0

Try this and define the scope of variable as global    

 DBDATA="";
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>';
            DBDATA=output;
            // console.log(output);
            // return output;

        }
    ); 
  return  output ;
}
console.log(DBDATA);
exports.alldatas = alldata();
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • Not working ... how we can define the scope of variable as global? – Bineet Chaubey Aug 14 '13 at 05:31
  • http://stackoverflow.com/questions/5447771/node-js-global-variables http://stackoverflow.com/questions/9765215/global-variable-in-app-js-accessible-in-routes And See mu updated answer – M Khalid Junaid Aug 14 '13 at 11:00
  • 1
    @dianuj You are still accessing the `output` outside the callback passed to `client.query` function. The output won't be accessible there as I explained in my answer – Salman Aug 14 '13 at 11:16