我知道node.js是事件驱动的,我应该这样做异步但我无法在脑海中找到一种方法。
所以,我有这个
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";
}
});
使其同步的问题是内部问题。 基本上我在结果变量中生成一个文档,我检查组并告诉成员所以预期的输出是
Group1 = member, member
Group2 = member, member
答案 0 :(得分:3)
对于这类问题,我通常使用类似下面的模式。简而言之:获取一个事物列表然后调用一个函数来处理列表;该函数调用自身直到列表完成;你在累加器中收集结果;当列表为空时,返回通过回调累积的任何内容。这只是完成@Andrey Sidorov在回应中所表现出来的另一种方式。
//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);
}
});
}
}
答案 1 :(得分:1)
function do_queries( resultCallback )
{
var querystr = "SELECT * FROM groups";
var result = "";
var num_queries_left = 0;
dbClient.query(querystr, function (err, res, fields) {
if (err) {
console.log(err);
return;
}
// calculate number of invocations of second sub-query
for(var i in res)
num_queries_left += res[i].members.split(",").length;
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;
num_queries_left--;
if (num_queries_left == 0)
{
resultCallback(result);
return;
}
});
}
result = result + "\n";
}
});
}
do_queries( function(result) {
console.log(result);
});