mysql - Nodejs Mysql 回调 hell

标签 mysql node.js asynchronous callback

我在nodejs中使用异步与mysql来从数据库注入(inject)数据。

所以我无法将数据从mysql结果传递到mysql端来打印出来

查看它解释的这段代码

    var users = [] ;

      .on('result', function(user,callback) {
    async.waterfall([
    function(cb){
        get_comm(user.post_id ,function(moredata){

            user.moredata = moredata;
            users.push(user);
            cb(users);

        })
    },

    ],function(users , cb){

    console.log(users); // it prints out successfully 
    });


})
.on('end', function() {
  if (connectionsArray.length) {
    pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
                console.log(users); // it prints out empty array [] !!

 updateSockets({
      users: users // empty array [] !!
    });


  }
});

最佳答案

我不确定 waterfall 功能是否最适合您,您也可以使用each或eachSeries https://github.com/caolan/async ,如果您确实有一堆用户 ID。

示例:

userIDs = [1,3,42,23];
users = [];
async.each(users, function( user, callback) {
    get_comm(user,function(moredata){
      user.moredata = moredata;
      users.push(user);
      callback(users);
    })
  }
  }, function(err){
  if( err ) {
    console.log(err);
  } else {
    console.log(users);
  }
});

尝试一下,我不确定这是否是你想要的,你的问题很难理解。

关于mysql - Nodejs Mysql 回调 hell ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27027053/

相关文章:

jquery - 用 Monadic 咒语召唤 JQuery Deferred

multithreading - 异步代码 + Async.RunSynchronously 与普通同步代码

mysql - 如何从两个不同的表中获取两个不同的列值 invert 一个值表依赖于另一个表值

php - 转换mysql时间格式00 :00:00 to 00:00 AM/PM with PHP

javascript - 如何让我的 Node/Socket.io 聊天在域中进行通信?

node.js - 如何在 Mongoose 中推送数组

node.js - block 不适用于 nodejs 和 Jade

c# - 异步 Controller 不会循环/不会被调用

Mysql自内连接查询?

mysql - 是否可以在安装完 php、mysql 等之后再安装 Sphinx?