node js redis循环遍历每个哈希键值

时间:2014-04-23 14:43:02

标签: javascript node.js express redis hogan.js

我对redis& amp;节点,此刻我正在尝试遍历我创建的一些测试哈希键并打印出来以筛选结果。以下是我期望看到的结果:

{ "aaData": [['Tim Osbourne'],['Joe Bloggs'],['John Doe'],['Perry Coke'],['Will Holmes'],['Steven Smith']}

但我得到了这个结果:

{ "aaData": [[],[],[],[],[],[],]}'Tim Osbourne','Joe Bloggs','John Doe','Perry Coke','Will Holmes','Steven Smith',

这是我的代码:


    app = require('../app');
    var redis = require("redis"),
    client = redis.createClient();
    routes = require('./');
    var key_types = '';

    client.keys("*", function (err, all_keys) {
        key_types += '{ "aaData": [';

        all_keys.forEach(function (key, pos) { // use second arg of forEach to get pos      
            key_types += "[";

            client.hmget([key, 'Owner of space'], function(err, field_val){
                key_types = key_types + "'" + field_val + "',";
            });

            key_types += "],";
        });

        key_types += "]}";               
    });

    app.get('/table_data', function(req, res){
        res.render('table_data', { keys: key_types});
    });

1 个答案:

答案 0 :(得分:0)

  • 你不应该keys *
  • 它不起作用,因为hmget是异步的,你应该使用async模块(async.map)。

[key, 'Owner of space']的目标是什么,因为'Owner of space'始终会产生相同的结果?