Node.js中的同步mysql [英] Synchronous mysql in Node.js

查看:261
本文介绍了Node.js中的同步mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道node.js是事件驱动的,我应该异步执行此操作,但是我在脑海中找不到实现此目的的方法.

I know that node.js is event driven and I should do this async but i can't find in my mind a way to do it.

所以,我有这个

var querystr = "SELECT * FROM groups";
    var result = "";
    dbClient.query(querystr, function (err, res, fields) {
        if (err) {
            console.log(err);
            return;
        }
        for(var i in res) {
            result = result + "@" +res[i].name + " =";
            for (var j in res[i].members.split(",")) {
                var memberquery;
                if (j.substr(0,1) == "@") {
                    memberquery = "SELECT name FROM groups WHERE id = "+j.substr(1, j.length-1);
                } else {
                    memberquery = "SELECT username FROM users WHERE id= "+j;
                }
                dbClient.query(memberquery, function(err, memres, fields) {
                    var membername = "";
                    if (typeof memres[0].username == "undefined") {
                        membername = "@"+memres[0].name;
                    } else {
                        membername = memres[0].username;
                    }
                    result = result + " " + membername; 
                });

            }
            result = result + "\n"; 
        }
    });

使其同步的问题是for内部. 基本上我是在结果变量中生成文档,在其中检查组并告诉成员,这样预期的输出是

The issue that makes it sync is the for inside. basically i'm generating a document in the result variable where i check the groups and tell the members so the expected output is

Group1 = member, member
Group2 = member, member

推荐答案

对于这种类型的问题,我通常使用以下模式.简而言之:获取事物列表,然后调用函数来处理该列表;该函数将自行调用,直到列表完成为止;在累加器中收集结果;当列表为空时,返回您通过回调积累的所有信息.这只是完成@Andrey Sidorov在其回应中展示的内容的另一种方式.

I usually use a pattern like the one below for this type of problem. In a nutshell: get a list of things then call a function to handle the list; that function calls itself until the list is completed; gather up the results as you go in an accumulator; when the list is empty, return whatever you've accumulated through the callback. It's just another way of accomplishing what @Andrey Sidorov demonstrated in his response.

//cb is (err, res) 
function getData(cb){
  var querystr = "SELECT * FROM groups";
  var result = "";

  dbClient.query(querystr, function (err, res, fields) {
      if (err)
          cb(err);
      else {
        var groups = [];
        for (var ndx in res)
          groups = groups.concat(res[ndx].members.split(","));

        getMembers(groups, [], cb);
      }
  });
}

function getMembers(members, sofar, cb){
  var member = members.shift();

  if (!member)
    cb(null, sofar);
  else {
    var memberquery;
    var params;

    if (member.substr(0,1) == "@") {
        memberquery = "SELECT name FROM groups WHERE id = ?";
        params = [member.substr(1, member.length-1)];
    } else {
        memberquery = "SELECT username FROM users WHERE id = ?";
        params = [member];
    }

    dbClient.query(memberquery, params, function(err, res) {
      if (err)
        cb(err);
      else {
        var membername = "";
        if (typeof res[0].username == "undefined") {
            membername = "@" + res[0].name;
        } else {
            membername = res[0].username;
        }

        sofar.push(membername);

        getMembers(members, sofar, cb);
      }
    });
  }
}

这篇关于Node.js中的同步mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆