0

I am new to node.js and express.js. I know this is a little bit silly, but I really don't know how to solve this problem. Hope there is someone can help me.

I have some information stored in Redis.

redis 127.0.0.1:6379> hgetall "store1"
1) "apple"
2) "10"
3) "banana"
4) "15"
5) "pear"
6) "20"
7) "name"
8) "A Street"
redis 127.0.0.1:6379> hgetall "store2"
1) "apple"
2) "30"
3) "banana"
4) "40"
5) "pear"
6) "50"
7) "name"
8) "B Street"

I want to show these information in a table. Using express.js, I created the following routes file.

routes/report.js

var redis = require('redis'),
    redisclient = redis.createClient();

exports.index = function(req, res){
  redisclient.on("error", function (err) {
      console.log("Error " + err);
  });

  var reports = [];
  redisclient.keys("*", function(err, stores) {
    for (var store in stores) {
      redisclient.hgetall(store, function(err, figures) {
        reports.push(figures);
      });
    }
  });

  res.render('report', { title: 'Store Report', reports: reports });
};

And also the following jade view files.

views/report.jade

h1= title
table(class="table table-striped table-condensed")
  thead
    tr
      th store
      th apple
      th banana
      th pear

  tbody
  - each report in reports
    !=partial('partials/record', {store:record.name, apple:record.apple, banana:record.banana, pear:record.pear})

views/partials/record.jade

tr
  td= store
  td= apple
  td= banana
  td= pear

When I open localhost:3000/report, I get the table structure with no content.

I understand that redis call is async. The report.js file rendered the report.jade before redis return any result.

Would anyone please to tell me how can I solve this problem?

Thanks!

4

1 回答 1

1

尝试使用async模块,在所有 IO 操作完成后执行回调以呈现结果。

来自异步 README 的代码示例:

async.parallel([
    function(){ ... },
    function(){ ... }
], callback);
于 2012-05-21T04:12:25.147 回答