If you’re getting an error that reads “ERR wrong number of arguments for ‘smembers’ command” in Redis, it’s because you’re calling the SMEMBERS
command with the wrong number of arguments.
To fix this issue, make sure you’re passing the correct number of arguments. At the time of writing, the correct number of arguments for this command is one.
Example of Error
Here’s an example of code that produces the above error:
SMEMBERS animals Dog
Result:
(error) ERR wrong number of arguments for 'smembers' command
In this case, I passed two arguments, but I should have passed just one.
We get the same error when we don’t pass any arguments:
SMEMBERS
Result:
ERR wrong number of arguments for 'smembers' command
Solution
To fix the issue, simply pass the correct number of arguments.
Example:
SMEMBERS animals
Result:
1) "Cat" 2) "Dog" 3) "Mouse" 4) "Zebra"
Here, I provided the correct number of arguments and we got the desired result without error.
At the time of writing, SMEMBERS
accepts one argument. This argument is the key that contains the set that we’re trying to retrieve all members of. See the Redis documentation to check the latest specification for this command (in case anything changes after this article is published).