New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
da99 opened this Issue Feb 1, 2013 · 4 comments

Comments

Projects
None yet
4 participants
@da99

da99 commented Feb 1, 2013

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

This comment has been minimized.

Show comment
Hide comment
@brycebaril

brycebaril Feb 3, 2013

Member

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
Member

brycebaril commented Feb 3, 2013

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

This comment has been minimized.

Show comment
Hide comment
@da99

da99 Feb 4, 2013

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 commented Feb 4, 2013

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 Feb 4, 2013

@BridgeAR BridgeAR reopened this Sep 14, 2015

@BridgeAR BridgeAR added the bug label Sep 14, 2015

BridgeAR added a commit that referenced this issue Sep 16, 2015

Merge pull request #838 from fintura/consistent-commands
Consistent commands arguments. All commands are from now on behaving the same no matter if they are on multi or no and they all take an array as either the first or second argument.

Fixes #686 #369 #422 #390 and Closes #634
@BridgeAR

This comment has been minimized.

Show comment
Hide comment
@BridgeAR

BridgeAR Sep 16, 2015

Member

This got closed by #838

Member

BridgeAR commented Sep 16, 2015

This got closed by #838

@bguiz

This comment has been minimized.

Show comment
Hide comment
@bguiz

bguiz Aug 16, 2016

For future reference, the following works now (tested on redis@2.6.2):

redisClient.hdel(key, [subkey1, subkey2], (err, result) => { /* ... */ });

bguiz commented Aug 16, 2016

For future reference, the following works now (tested on redis@2.6.2):

redisClient.hdel(key, [subkey1, subkey2], (err, result) => { /* ... */ });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment