简体   繁体   中英

how can find return variable value outside anonymous function in node js mysql query function

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 .

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM