node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

Parsing the response into the right format with eval

Open julien51 opened this issue 13 years ago • 8 comments

Hi! We use eval in one of our scripts. The script returns an Multibulk which should (at least we expected!) be translated into a JS object, rather than an array. For some reason eval yields an array. We assume that's because node_redis has no way to know exactly what to expect from an eval. Is there a way to tell it that it's going to be a hash?

julien51 avatar Jul 18 '13 07:07 julien51

http://redis.io/commands/eval

See "Conversion between Lua and Redis data types".

MetaThis avatar Jul 18 '13 18:07 MetaThis

Wel, yes, but what I'm interested in is conversion between Redis and Javascript types :) When hgetall returns a Multi bulk response, node_redis converts it to a hash, when eval returns a Multi bulk response, node_redis converts it to an array.

julien51 avatar Jul 18 '13 18:07 julien51

@julien51 Could you provide an example script?

skenqbx avatar Jul 19 '13 12:07 skenqbx

Yes;

  var redis = require("redis"),
  client = redis.createClient();


  client.on("error", function (err) {
    console.log("Error " + err);
});

  client.hmset("hash", "k1", "v1", "k2", "v2", function(e, o) {
    console.log(e, o);
    client.hgetall("hash",  function(e, o) {
        console.log(e, o);
        client.eval("return redis.call('hgetall', KEYS[1])", 1, "hash", function(e, o) {
            console.log(e, o);
        });
    });

});

This will yield the following:

$ node redisdebug.js
null 'OK'
null { k1: 'v1', k2: 'v2' }
null [ 'k1', 'v1', 'k2', 'v2' ]

I would expect both the 2nd and 3rd line to be the same because obviously they're both the result of an hgetallcall.

julien51 avatar Jul 23 '13 12:07 julien51

Ahh -- the javascript client's hgetall command is special-cased to convert the reply into an object. As the client has no way to know what the return type of an eval command is, it doesn't know what to give you, and just gives you the raw redis reply, which is key1, value1, key2, value2.

brycebaril avatar Jul 24 '13 04:07 brycebaril

Exactly. Now, node_redis does the conversion internally. It'd just be nice if there was an option to tell node_redis: this is an eval, please yield an object.

julien51 avatar Jul 24 '13 17:07 julien51

The refactoring work I'm doing should support something like this, but it will still be up to the user, as not all eval scripts return objects.

brycebaril avatar Jul 24 '13 17:07 brycebaril

I would be open for a pull request for this.

BridgeAR avatar Oct 15 '15 18:10 BridgeAR