javascript - 如何在 express.js 中呈现 redis 记录?

标签 javascript node.js redis express

我是 node.js 和 express.js 的新手。我知道这有点傻,但我真的不知道如何解决这个问题。希望有人能帮助我。

我有一些信息存储在 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"

我想在表格中显示这些信息。使用 express.js,我创建了以下路由文件。

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 });
};

还有以下 jade View 文件。

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

当我打开 localhost:3000/report 时,我得到没有内容的表结构。

我知道 redis 调用是异步的。 report.js 文件在 redis 返回任何结果之前渲染了 report.jade

谁能告诉我如何解决这个问题?

谢谢!

最佳答案

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

异步自述文件中的代码示例:

async.parallel([
    function(){ ... },
    function(){ ... }
], callback);

关于javascript - 如何在 express.js 中呈现 redis 记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10679363/

相关文章:

javascript - 如何使用 AngularJS 通过已检查的值检查复选框?

javascript - 将对象及其方法分配给原型(prototype)

javascript - 根据标签值html增加图像边距

javascript - 为什么我删除的函数在 Node.js 中不是 typeof "undefined"?

caching - 使用 Redis 的无界多维索引

redis - 所有实体的 Spring Data Redis 全局 TTL

javascript - 具有来自 JSON 的多个系列的 Highcharts

node.js - 如何使用 Node.JS 的 restify 框架解析/读取多个参数

node.js - 如何在没有任何 babel 依赖的情况下发布 npm 模块?

Redis 客户端未通过 HAProxy 连接到 Redis 服务器