1

This is day one for me with redis cilent and node js.

I'm trying to replicate this command that I run via the redis-cli in node.js:

127.0.0.1:6379> HGETALL widgets:9145090003_00:00_00:00
1) "id"
2) "33305"
3) "loc"
4) "jamaica"
5) "days"
6) ""

Here's the nodejs code:

client.hgetall("widgets:9145090003_00:00_00:00", function (err, replies) {
        console.log(err);
        replies.forEach(function (reply,i) {
                console.log('  ' + i + ':' + reply);
        }); 
});

It comes back null and then fails while trying to do the foreach.

i also tried:

 client.hgetall("widgets", function (err, replies) {
            console.log(err);
            replies.forEach(function (reply,i) {
                    console.log('  ' + i + ':' + reply);
            }); 
    });

But I get the same result.

Not sure what I'm doing wrong.

EDIT 1

I tried this code:

 17 console.log("attempting to do hgetall..");
 18 client.hgetall("widgets:9145090003_00:00_00:00", function (err, replies) {
 19         //for (let k in replies) console.log(' ${k}:${replies[k]}');
 20         console.log(replies.id);
 21 });

And here are the results:

/var/www/html/node/test/models/Widgets.js:20
    console.log(replies.id);
                       ^

TypeError: Cannot read property 'id' of null

And yet, in the CLI i can do this:

127.0.0.1:6379> HGET widgets:9145090003_00:00_00:00 id
"33305"
127.0.0.1:6379> HGETALL widgets:9145090003_00:00_00:00
1) "id"
2) "33305"
3) "loc"
4) "jamaica"
5) "days"
6) ""
127.0.0.1:6379> 
Happydevdays
  • 1,982
  • 5
  • 31
  • 57

3 Answers3

1

The reply of hgetall is of type Object, Try adding the following line to your code

console.log(replies.id);

and you will print 33305 in your console.

marvel308
  • 10,288
  • 1
  • 21
  • 32
0

I tried to replicate what you were doing. I would first check if you're connecting to the correct instance of redis. If you are, the problem is that the forEach function works for arrays, but replies here is an object and there is no Object.prototype.forEach function. You would have to loop over the response differently. I've re-written your code in a way that should work if you're connected to the correct instance of redis.

client.hgetall("widgets:9145090003_00:00_00:00", function (err, replies) {
        console.log(replies);
        for(let k  in replies) {
           console.log(` ${k}:${replies[k]}`);
        }
});
Mike B
  • 2,756
  • 2
  • 16
  • 28
  • where should i have checked to see that this specific method in the redis client returns an object? just asking for future reference to help myself. this is the reference I've been using ... which is very limited: https://www.npmjs.com/package/redis – Happydevdays Sep 13 '16 at 13:57
  • and btw, I tried this code but it returns nothing. No errors, but nothing prints. – Happydevdays Sep 13 '16 at 14:03
  • it just seems like you're not connecting properly. I think you should read [this before proceeding](https://github.com/NodeRedis/node_redis#rediscreateclient), it'll tell you how to connect to the port on which your redis instance is listening. As for knowing what the functions return, I think you should look at the documentation for redis itself. Here since you're calling hgetall, you'll be returning a hash which translates to an object in javascript – Dimeji Abidoye Sep 13 '16 at 17:50
0

For the devs who uses async/await: Do not forget to add await before the method returning the values from Redis or you will get an empty array/set.

Example:

 await this.client.zRange('test_queue', '0', '100')
Mehdi Fracso
  • 448
  • 2
  • 16