Happydevdays Happydevdays - 2 months ago 16
Node.js Question

redis client and node js - hgetall method fails with empty results

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>

Answer

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.