Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

What is the proper way of using HDEL with an array of fields? #369

Closed
da99 opened this Issue · 2 comments

2 participants

@da99

Is HDEL supposed to be used as the following?:

client.hdel( my_key, [ field1, field2, ...] , callback );

or:

client.hdel( [ my_key, field1, field2, ...] , callback );

Right now, with node-redis 0.8.2, (no hiredis), the second approach works perfectly, The first approach only works with an array of length 1: [ field1 ]. Is this the correct behaviour? Or should it be the first approach?

Here is a more complete code example:

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

client.hmset("my_hash", {a: "a", b: "b"}, function (e, r) {
  redis.print(e,r);
  client.hmset("my_hash", {c: "c", d: "d"}, function (e, r) {
    redis.print(e,r);
    client.hdel('my_hash', ['a', 'c', 'd'], function (e, r) {

      redis.print(e,'This does not work: instead of 3 we get: ' + r);
      client.hdel("my_hash", 'a', 'c', 'd', function (e, r) {
        redis.print(e,'We get what we want using this approach: ' + r);
        client.quit();
      });
    });
  });
});
@brycebaril
Collaborator

TL;DR: yes, for now you'll need to use your second example or call client.send_command("hdel", [key, arg1, arg2], callback) directly.

There are similar issues existing with the smart parsing, see issue #292 and pull request #276

The reason the latter example works is because you're essentially just passing the array directly to send_command() as if you were doing this:

client.send_command("hdel", [ my_key, field1, field2, ...] , callback );

This is essentially how all commands are parsed and sent to Redis.

client.COMMAND("key", "arg1", "arg2", "arg3", callback);

using some arguments sniffing, this gets converted to:

client.COMMAND(["key", "arg1", "arg2", "arg3"], callback);
// let's call ["key", "arg1", "arg2", "arg3"] `args`

which is then dispatched to

client.send_command(COMMAND, args, callback);

then in send_command, if each arg isn't a string, arg.toString() is called as it is sent, which in the case of your first command means ["a", "b", "c"] turns into a,b,c:

*3
$4
hdel
$7
my_hash
$5
a,c,d

Whereas the syntax you need is:

*5
$4
hdel
$7
my_hash
$1
a
$1
c
$1
d
@da99

It all makes sense now thanks to your details on COMMAND and send_command. Thank you very much. That answered my question and any future ones I could possibly have.

@da99 da99 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.