First, a preliminary comment: you are not supposed to use the KEYS command in user application. This is a O(n) debug command. If you have many keys in Redis, it will freeze the instance at each call.
Now, regarding the program itself, you need to check the scoping rules of Javascript (the ones that apply to closures). Here you access to key variable in the hgetall callback, but it has not been properly captured in a closure (a for loop does not define a closure, you need a function for that).
Here is a possible fix:
redis.hmset("this:that:a", {"one": 'two', "three": 'four'});
redis.hmset("this:that:b", {"five": "six", "seven": "eight"});
var all_parts = {};
redis.keys("this:that:*", function(err, keys) {
var count = keys.length;
keys.forEach( function(key) {
redis.hgetall(key, function(err, obj) {
all_parts[key] = obj;
--count;
if (count <= 0) {
console.log(all_parts);
} else {
console.log('waiting');
}
});
});
});
returns:
waiting
{ 'this:that:b': { five: 'six', seven: 'eight' },
'this:that:a': { one: 'two', three: 'four' } }
See more information at:
hdel inside hget block nodejs redis