2

I have Python code:

redis.sadd(r_key, *set(r_list))

and it works excellent: But I can't do the same on Node.js

redis.sadd(r_key, new Set(array), function(err) {})

What am I doing wrong ?

2 Answers2

1

It depends on the library you are using
You can use IOREDIS for node, where you can pass the array as an input parameter.

redis.sadd('r_key', 1, 3, 5, 7);
redis.sadd('r_key', [1, 3, 5, 7]);
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

Actually, this is a problem about how to use flatten args in Js like python *.

You can read this doc https://www.npmjs.com/package/redis, It said, all the redis api is 1 to 1 mapping of redis server command. so SADD is :

 SADD key member [member ...]

Your js code should be like above. just a case:

 redis.sadd("key","member", "member", ...) 

So you know why you code does not work.

To do this, you can refer this question : Python-like unpacking in JavaScript

 redis.sadd.apply(this, args)

And the args is a array of key and your members string .

GuangshengZuo
  • 4,447
  • 21
  • 27