multi.hmget not working when passing in an array of hash keys. #389

Closed
Jezternz opened this Issue Mar 3, 2013 · 7 comments

Projects

None yet

8 participants

@Jezternz
Jezternz commented Mar 3, 2013

Hi,
I am using the latest version of node-redis on npm (0.8.2 https://npmjs.org/package/redis),
Anyway when I call the below commands everything is peachy:

redisClient.hmget('{key}', ['{hashkey1}', '{hashkey2}']) => [val1, val2]
redisClient.hmget('{key}', '{hashkey1}', '{hashkey2}') => [val1, val2]
multi.hmget('{key}', '{hashkey1}', '{hashkey2}') => [val1, val2]

But when I call hmget with multi and feed an array of keys as the second paramater (below), all I get back is null.

multi.hmget('{key}', ['{hashkey1}', '{hashkey2}']) => [null]

As this is all through my code, this is rather inconvenient, it would be great if you could fix this and update npm, as I will have to revert to an older version until this is fixed.

Cheers

@Jezternz Jezternz closed this Mar 3, 2013
@Jezternz Jezternz reopened this Mar 3, 2013
@brycebaril
Member

Hi @Jezternz -- there are some multi parser bug fixes that should go out in the next version. This may be an instance of one of the bugs that #382 will fix. You could try your example with that PR to see if it fixes your issue.

@ionian-wind

multi.hmget('{key}', ['{hashkey1}', '{hashkey2}']) => [null]

Well, all the same for current moment. =(

The workaround is to use multi.hmget like that:
multi.hmget(['{key}', '{hashkey1}', '{hashkey2}'])

@brycebaril
Member

Ahh, yes, this is because the list is stringified. Multi isn't getting the same HMGET command parsing logic that you get without multi. See https://github.com/mranney/node_redis/blob/master/index.js#L968

This sort of thing is exactly one of the things I'm working on fixing in the refactor.

> c.multi().hmget("somehash", ["cat", "cow"]).exec(console.log)
send 127.0.0.1:6379 id 2: *1
$5
MULTI

send_command buffered_writes: 0  should_buffer: false
send 127.0.0.1:6379 id 2: *3
$5
hmget
$8
somehash
$7
cat,cow

send_command buffered_writes: 0  should_buffer: false
send 127.0.0.1:6379 id 2: *1
$4
EXEC

vs.

c.multi().hmget("somehash", "cat", "cow").exec(console.log)
send 127.0.0.1:6379 id 2: *1
$5
MULTI

send_command buffered_writes: 0  should_buffer: false
send 127.0.0.1:6379 id 2: *4
$5
hmget
$8
somehash
$3
cat
$3
cow

send_command buffered_writes: 0  should_buffer: false
send 127.0.0.1:6379 id 2: *1
$4
EXEC
@barisusakli

I just ran into this and the following worked for me.

function getMultiFields(key, fields, callback) {
    var multi = redis.multi();
    multi.hmget.apply(multi, [key].concat(fields));
    multi.exec(callback);
}
@julianlam

Oh hey @barisusakli, fancy seeing you here. Your code worked for me. (In my case, I accidentally defaulted to using null instead of multi for the thisArg in .apply(). Changing it to multi fixed it up)

@blainsmith blainsmith closed this Aug 15, 2015
@BridgeAR
Member

This is fixed in newer versions

@katopz
katopz commented Oct 5, 2016

So what final function look like? I saw

            it('returns strings for keys requested', function (done) {
                client.hmget('hash key 2', 'key 1', 'key 2', function (err, reply) {
                    assert.strictEqual(true, Array.isArray(reply));
                    assert.strictEqual(2, reply.length);
                    assert.strictEqual('val 1', reply[0]);
                    assert.strictEqual('val 2', reply[1]);
                    return done(err);
                });
            });

but not this anywhere

client.hmget('hash key 2', ['key 1', 'key 2'], function (err, reply)  {}

In my case ['key 1', 'key 2'] is need because it will be vary and I can't find the way to make it work.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment