当我需要进行多次SELECT查询时,我想知道我是否在路由页面中正确执行此操作。
由于我需要使用数据呈现我的页面,因此我必须等待所有查询完成,否则它将呈现页面而不等待查询完成(因为它们是异步的)..
pool.getConnection(function(err, conn) {
var array = [];
var IDS = [];
var array2 = [];
var array3 = [];
//query 1
query = conn.query('SELECT * FROM table1 LIMIT 0, 5');
query.on('error', function(err){
throw err;
});
query.on('result', function(row){
array.push(row);
IDS.push(row.id);
});
query.on('end', function(result){
//query 2 with results from query 1
query = conn.query('SELECT * FROM table2 WHERE table1_id IN (?)', [IDS]);
query.on('error', function(err){
throw err;
});
query.on('result', function(row){
array2.push(row);
});
query.on('end', function(result){
//query 3
query = conn.query('SELECT * FROM table3');
query.on('error', function(err){
throw err;
});
query.on('result', function(row){
array3.push(row);
});
query.on('end', function(result){
res.render('index', {data1: array, data2: array2, data3: array3});
});
});
});
conn.release();
});
如您所见,我将第二个查询放在第一个查询的END stream
内,依此类推。
当我不得不提出更多疑问时,它开始成为一场噩梦。
我并不是在谈论每个if/else statement
+重复res.render
,当我使用上一个查询中的数据制作一个新的时,我必须使用conn.release()
查询没有结果我需要避免使用0结果制作第二个)。
要完成,我还想知道res.render
代码是放在正确的位置还是应该放在{{1}}之后?
感谢您的帮助
答案 0 :(得分:0)
所以我找到了async.js模块,它允许以parralel或串行方式运行函数。
这是我对该模块的第一次尝试(请纠正我,如果我做错了),它似乎工作正常。
不要查看我的SQL查询,因为我为了更好的视图而截断了一些。如果您想知道我的代码是什么:我使用它来显示提要(帖子)及其评论和类似系统(如提要,如评论)
在代码中,我使用:
db_result
也许我做错了因为我没有真正使用异步模块的任何回调,我所做的就是弹出我的全局数组......
var db_feedIDS = [];
var db_feedIndex = [];
var indexFeed = -1;
var db_commentsIDS = [];
var db_commentsIndex = [];
var indexComments = -1;
var lastComment = [];
var db_result = [];
async.series({
Feeds: function(callback){
pool.getConnection(function(err, conn) {
query = conn.query('SELECT * FROM feed LIMIT 0, 5');
query.on('error', function(err){
callback(err);
});
query.on('result', function(row){
indexFeed++;
db_feedIndex[row.id] = indexFeed;
db_feedIDS.push(row.id);
db_result.push({"id": row.id, "date": row.date, "content": row.content, "comments": [], "like":[]});
});
query.on('end', function(result){
conn.release();
callback(null, db_feedIDS.length);
});
});
},
Comments: function(callback){
if(db_feedIDS.length == 0){
callback(null, 0);
}else{
pool.getConnection(function(err, conn) {
query = conn.query('SELECT * FROM feed_comments, users WHERE feed_comments.feedid IN (?)', [db_feedIDS]);
query.on('error', function(err){
callback(err);
});
query.on('result', function(row){
if(!lastComment[row.id_comments]){
lastComment[row.id_comments] = [];
indexComments = -1;
}
indexComments++;
db_commentsIDS.push(row.id_comments);
db_commentsIndex[row.id_comments] = indexComments;
var a = {"id_comments": row.id_comments, "content_comments": row.content_comments, "date_comments": row.date_comments, "likeComment":[]};
db_result[db_feedIndex[row.feedid_comments]].comments.push(a);
});
query.on('end', function(result){
conn.release();
callback(null, db_commentsIDS.length);
});
});
}
},
Likes: function(callback){
async.parallel({
likes_feed: function(callback){
if(db_feedIDS.length == 0){
callback(null, 0);
}else{
pool.getConnection(function(err, conn) {
query = conn.query('SELECT * FROM feed_like, users WHERE feed_like.feed_id IN (?) AND users.id = feed_like.userid ORDER BY feed_like.id ASC', [db_feedIDS]);
query.on('error', function(err){
callback(err);
});
query.on('result', function(row){
var b = {"id_like": row.id_like, "userid_like": row.userid_like, "username_like": row.username_like, "img_thumb_like": row.img_thumb_like};
db_result[db_feedIndex[row.feedid_like]].like.push(b);
});
query.on('end', function(result){
conn.release();
callback(null, "ok");
});
});
}
},
likes_comment: function(callback){
if(db_commentsIDS.length == 0){
callback(null, 0);
}else{
pool.getConnection(function(err, conn) {
query = conn.query('SELECT * FROM feed_comments_like, users WHERE feed_comments_like.comment_id IN (?) AND users.id = feed_comments_like.userid ORDER BY feed_comments_like.id ASC', [db_commentsIDS]);
query.on('error', function(err){
throw err;
});
query.on('result', function(row){
var c = {"id_comments_like": row.id_comments_like, "feedid_comments_like": row.feedid_comments_like, "commentid_comments_like": row.commentid_comments_like, "userid_comments_like": row.userid_comments_like, "username_comments_like": row.username_comments_like, "img_thumb_comments_like": row.img_thumb_comments_like}
db_result[db_feedIndex[row.feedid_comments_like]].comments[db_commentsIndex[row.commentid_comments_like]].likeComment.push(c);
});
query.on('end', function(result){
conn.release();
callback(null, "ok");
});
});
}
}
},
function(err, results) {
callback(null, results);
});
}
},
function(err, results) {
console.log(results);
res.render('index', { title: "Blabla", db_feed: db_result });
});