perrycoke
perrycoke

Reputation: 45

node js redis loop through each hash key value

I am very new to redis & node and at the moment I am trying to loop through some test hash keys that i have created and print out to screen the results. Here is the result I expect to see:

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

but instead I get this result:

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

Here is my code:


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

Upvotes: 2

Views: 1233

Answers (1)

FGRibreau
FGRibreau

Reputation: 7239

  • You should not do a keys *
  • It does not work because hmget is asynchronous, you should use the async module (async.map) for that.

What is the goal of [key, 'Owner of space'] since 'Owner of space' will always yield the same result?

Upvotes: 0

Related Questions