简体   繁体   中英

Why doesn't redis SADD work with an array as an input in node.js?

I am using https://github.com/mranney/node_redis and trying to update a set with multiple values at once (on redis cli "SADD myset val1 val2" works fine).

The exact command I am using is:

var cmd_array = ['myset', 'val1', 'val2'];
client.sadd(cmd_array);

Based on their documentation this should of worked. Any idea why it doesn't?

Thanks

It works fine for me. I can run the following script without any issue:

var redis = require("redis"),
    client_options = {
    parser: "hiredis"
};

var credis = redis.createClient( '/tmp/redis.sock', client_options );

function main()
{
   credis.flushall( function(err,res) {
      console.log( "hello" );
      x = [ "X", "A", "B", "C" ];
      credis.sadd(x);
      credis.sadd( "myset", x, function(err,res) {
         console.log("done");
         credis.end();
      });
   });
}

main();

It creates the two keys with the expected number of items:

redis 127.0.0.1:6379> keys *                                                                                                                                                                                                                 
1) "X"                                                                                                                                                                                                                                       
2) "myset"                                                                                                                                                                                                                                   
redis 127.0.0.1:6379> smembers myset                                                                                                                                                                                                         
1) "X"                                                                                                                                                                                                                                       
2) "A"                                                                                                                                                                                                                                       
3) "B"                                                                                                                                                                                                                                       
4) "C"                                                                                                                                                                                                                                       
redis 127.0.0.1:6379> smembers X                                                                                                                                                                                                             
1) "A"                                                                                                                                                                                                                                       
2) "B"                                                                                                                                                                                                                                       
3) "C"                                                                                                                                                                                                                                       

I would suggest you check the node_redis version. The version I use is:

$ npm list
/home/dspezia/local/test_redis
├── hiredis@0.1.14 
└── redis@0.7.1 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM