expressjs mongodb在里面查询查询查询循环

时间:2016-10-13 00:33:59

标签: node.js mongodb express ejs

所以我使用此查询创建一个数组以传递给ejs以生成表行。但在我通过之前,我还有其他查询,例如引用ID或计数。但我数不清楚。我可能还有其他疑问。

router.get('/', function(req, res, next) {
    MongoClient.connect(MongoUrl, function(err, db) {

        var table_data = [];
        db.collection('categories').find().toArray(function(err, result) {

            for (var i = 0, len = result.length; i < len; i++) {
                var count;
                db.collection('articles').count({category_id: result[i].category_id}, function(err, count) {
                    count = count;
                });

                table_data[i] = {
                    "category_id" : result[i].category_id,
                    "category" : result[i].category,
                    "count" : count,
                    "date_last_imported" : result[i].date_last_imported,
                    "feed" : result[i].feed,
                    "url" : result[i].url,
                    "xml" : result[i].xml
                };

            }
            console.log(table_data);
            res.render('categories', {title:"Categories",table_data: table_data});
        });
    });
});

1 个答案:

答案 0 :(得分:1)

这是因为尚未获取count并且您已将其推送到未定义的数组中。如果您在查询中有很多查询,那么在这种情况下,您将被困在回调地狱中。要摆脱这种情况,请使用promise或使用&#34; async&#34; module。

供参考:http://caolan.github.io/async/

对于当前场景并了解为什么&#34;计算&#34;未定义:

router.get('/', function(req, res, next) {
    MongoClient.connect(MongoUrl, function(err, db) {

        var table_data = [];
        db.collection('categories').find().toArray(function(err, result) {

            for (var i = 0, len = result.length; i < len; i++) {
                var count;
                db.collection('articles').count({category_id: result[i].category_id}, function(err, count) {
                    count = count;
                table_data.push( {
                    "category_id" : result[i].category_id,
                    "category" : result[i].category,
                    "count" : count,
                    "date_last_imported" : result[i].date_last_imported,
                    "feed" : result[i].feed,
                    "url" : result[i].url,
                    "xml" : result[i].xml
                });
             if(table_data.length === result.length) {
               res.render('categories', {title:"Categories",table_data:  table_data});
             }
                 });



            }

        });
    });
});

<强>更新 要摆脱内心的疑问。

router.get('/', function(req, res, next) {
    MongoClient.connect(MongoUrl, function(err, db) {
        var table_data = [];
        db.collection('categories').find().toArray(function(err, result) {
          async.each(result, function (user, cb) {
            async.waterfall([
              function (cb) {
                db.collection('articles').count({
                    category_id: result[i].category_id
                }, function(err, count) {
                    cb(err, count);
                });
              },
              function (count, cb) {
                //another query
                cb(err, count, secondquerydata);
              },
              .....
            ], function (err, count, secondquerydata, .....) {
              if(err) return callback(err);
              // here use your all query data
              table_data.push( {
                  "category_id": result[i].category_id,
                  "category": result[i].category,
                  "count": count,
                  "date_last_imported": result[i].date_last_imported,
                  "feed": result[i].feed,
                  "url": result[i].url,
                  "xml": result[i].xml
              });
              res.render('categories', {
                  title: "Categories",
                  table_data: table_data
              });
            })
          })

        });
    });
});