It's unclear to me whether you are fetching all the properties of the hash, in which case you should use hgetall
which returns an object. Otherwise, you want an interface looking something like this:
hmgetObject(redis)('key', 'name', 'date', ..., function (err, reply) {
// Here, reply is already an object
res.jsonp(reply);
});
You'll note how passing the redis client like this lets you easily reuse the function:
var hmget = hmgetObject(redis);
hmget('key1', 'name', 'date', ...);
hmget('key2', 'foo', 'bar', ...);
Here is the implementation, which is generic and works for an arbitrary number of arguments):
function hmgetObject (redis) {
return function () {
var args = [].slice.call(arguments, 0),
cb = args.pop();
args.push(map(args.slice(1), cb));
redis.hmget.apply(redis, args);
}
function map (props, cb) {
return function (err, vals) {
if(err) return cb(err);
var obj = {};
for(var i = 0, l = props.length; i < l; i++) {
obj[props[i]] = vals[i];
}
cb(null, obj);
}
}
}