Node.js 关于主表和子表的异步查询,有更好的解决方案吗?

Node.js 关于主表和子表的异步查询,有更好的解决方案吗?,node.js,asynchronous,express,node-mysql,Node.js,Asynchronous,Express,Node Mysql,我有两个表,tb_article和tb_attach_url,如下所示 ==============tb_article================= id title content 1 "echo" "test" 2 "foo" "bar" ================tb\U附加\U url============== id article_id url_val 1 2

我有两个表,tb_article和tb_attach_url,如下所示

==============tb_article=================
id      title       content
1       "echo"      "test"
2       "foo"       "bar"
================tb\U附加\U url==============

id      article_id      url_val
1       2               "http://.../foo.png"
2       2               "http://.../bar.png"
3       1               "http://.../test.png"
我的情况是这样的,, 我想展示一份tb_文章的列表, 但是tb_attach_url的信息也需要

我的解决方案如下:

function load_article_urls(id, idx, callback){
    connection.query("select url_str from tb_attach_url where article_id = ?", [id], function(err, results){
        if (err) throw err;

        var images = [];
        for (var j = 0; j < results.length; j++){
            images.push(results[j]["url_str"]);
        }
        callback(idx, images)
    });
}

function load_article(req, res){
    connection.query('select * from tb_article where id = ?', [req.query.id], function(err, results){
        if (err) throw err;

        var cnt = 0;
        for (var i = 0; i < results.length; i++){

            load_article_urls(results[i].id, i, function(idx, urls){

                results[idx]["urlset"] = urls;

                cnt++;
                if (cnt == results.length){
                    //waiting for all back
                    res.render('list_article', { article_list: results});
                }
            });
        }
    });
}
async.map(results,
  function (err, callback) { /* Call callback with the urls as a parameter here */ },
  function (err, allUrls) {
    // Combine results and allUrls - they are in the same order.
})
函数加载\u文章\u URL(id、idx、回调){
connection.query(“从tb\u attach\u url中选择url\u str,其中article\u id=?”,[id],函数(err,results){
如果(错误)抛出错误;
var图像=[];
对于(var j=0;j
我觉得这个工具有点难看,我想知道一些更好的方法。
谢谢您的帮助。

您可以使用
async.map
()执行以下操作:

function load_article_urls(id, idx, callback){
    connection.query("select url_str from tb_attach_url where article_id = ?", [id], function(err, results){
        if (err) throw err;

        var images = [];
        for (var j = 0; j < results.length; j++){
            images.push(results[j]["url_str"]);
        }
        callback(idx, images)
    });
}

function load_article(req, res){
    connection.query('select * from tb_article where id = ?', [req.query.id], function(err, results){
        if (err) throw err;

        var cnt = 0;
        for (var i = 0; i < results.length; i++){

            load_article_urls(results[i].id, i, function(idx, urls){

                results[idx]["urlset"] = urls;

                cnt++;
                if (cnt == results.length){
                    //waiting for all back
                    res.render('list_article', { article_list: results});
                }
            });
        }
    });
}
async.map(results,
  function (err, callback) { /* Call callback with the urls as a parameter here */ },
  function (err, allUrls) {
    // Combine results and allUrls - they are in the same order.
})
或者使用
async
的一些其他功能来实现类似的效果