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

扶醉桌前 提交于 2019-12-11 20:12:11

问题


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 .


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/18208766/how-can-find-return-variable-value-outside-anonymous-function-in-node-js-mysql-q

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!