If you’re getting an error that reads “ERR syntax error” when using the HRANDFIELD
command in Redis, it could be that you’re passing the wrong number of arguments. At the time of writing, the HRANDFIELD
command requires at least one argument, and accepts up to three arguments.
I find that this error only occurs when I pass too many arguments, but not when I pass too few (i.e. none). If I don’t pass any arguments, I get a different error that tells me I’ve passed the wrong number of arguments.
If you’re getting this error, check the number of arguments that you’re passing and adjust if required.
Example of Error
Here’s an example of code that produces the error:
HRANDFIELD pet 2 WITHVALUES OOPS
Result:
(error) ERR syntax error
In this case I passed four arguments, but the command accepts a maximum of three arguments (at the time of writing).
Solution
To solve this issue, we need to reduce the number of arguments:
HRANDFIELD pet 2 WITHVALUES
Result:
1) "color" 2) "Brown" 3) "age" 4) "37"
This time we get the result we’re looking for.
In this example I’m passing three arguments, but I could have used less (depending on my requirements). The main reason I got the above error was that I was passing too many arguments.