使用具有多个查询的Node.js构建JSON

时间:2013-08-23 05:57:16

标签: javascript node.js asynchronous

我刚开始用node.js编码,我知道node.js是异步的,但不知道如何处理这个问题。

我正在查询mysql并按如下方式构建JSON,

var mysql = require('mysql');
var connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password : 'root',
    port : '3306',
    database : 'tango_prod'
});

connection.connect();
var postsJSON = { };
var arr = new Array();

connection.query('SELECT * FROM posts LIMIT 100', function(err, rows) {
    for(i=0; i < rows.length; i++){
        var post_obj = {};
        var post = rows[i];
        post_obj.id = post.id
        post_obj.actor = build_actor(post.author_id); 
        arr.push(post_obj);
    }
    console.log(JSON.stringify(arr));
});

function build_actor(actor_id){
    connection.query('SELECT * FROM people WHERE id ='+actor_id+';', function(err, actor) {
        connection.query('SELECT * FROM users WHERE id ='+actor.owner_id+';', function(err, user) {
          var actor_obj = {};
          actor_obj.id = user.id;
          actor_obj.name = user.name;
          actor_obj.email = user.email;
          return actor_obj
        });
    });
}

connection.end();

我正在获取JSON输出,后来调用了build_actor函数。我必须在构建actor对象后才能获得JSON。

2 个答案:

答案 0 :(得分:3)

您必须使用回调异步执行此操作:

connection.query('SELECT * FROM posts LIMIT 100', function(err, rows) {
    for(i=0; i < rows.length; i++){
        build_actor(post.author_id, function(actor){
            var post_obj = {};
            var post = rows[i];
            post_obj.id = post.id
            post_obj.actor = actor;
            arr.push(post_obj);
            if(arr.length === rows.length)
                 console.log(JSON.stringify(arr));
        });
    }
});

function build_actor(actor_id, callback){
    connection.query('SELECT * FROM people WHERE id ='+actor_id+';', function(err, actor) {
        connection.query('SELECT * FROM users WHERE id ='+actor.owner_id+';', function(err, user) {
          var actor_obj = {};
          actor_obj.id = user.id;
          actor_obj.name = user.name;
          actor_obj.email = user.email;
          callback(actor_obj);
        });
    });
}

答案 1 :(得分:1)

 var mysql = require('mysql');
 var connection = mysql.createConnection({
 host : 'localhost',
 user : 'root',
 password : 'root',
 port : '3306',
 database : 'tango_prod'
});

connection.connect();
var postsJSON = { };
var arr = new Array();

connection.query('SELECT * FROM posts LIMIT 100', function(err, rows) {
  for(i=0; i < rows.length; i++){
     var post_obj = {};
     var post = rows[i];
     post_obj.id = post.id
     build_actor(post.author_id,function(err,res){

      if(!err){
             post_obj.actor=res;
             arr.push(post_obj);   
             console.log(JSON.stringify(arr));         
        }

     }); 

}

});

function build_actor(actor_id,cb){
    connection.query('SELECT * FROM people WHERE id ='+actor_id+';', function(err, actor) {
    connection.query('SELECT * FROM users WHERE id ='+actor.owner_id+';', function(err, user) {
       var actor_obj = {};
       actor_obj.id = user.id;
       actor_obj.name = user.name;
       actor_obj.email = user.email;
       connection.end();
       cb (null,actor_obj);
    });
});
}