Fix “ERR wrong number of arguments for ‘hstrlen’ command” in Redis

If you’re getting an error that reads “ERR wrong number of arguments for ‘hstrlen’ command“, it’s because you’re passing the wrong number of arguments when using the HSTRLEN command.

The HSTRLEN command requires two arguments (at the time of writing). These are the name of the key and the name of the field.

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:

HSTRLEN customer

Result:

(error) ERR wrong number of arguments for 'hstrlen' command

In this case, I didn’t pass enough arguments. I only passed one argument when I should’ve passed two.

We get the same error when there are too many arguments:

HSTRLEN customer height weight

Result:

(error) ERR wrong number of arguments for 'hstrlen' command

In this case I passed three arguments when I should’ve passed just two.

Solution

To fix this issue, be sure to pass the correct number of arguments. At the time of writing, the HSTRLEN command requires two arguments – the name of the key, and the name of the field for which to return the string length.

Therefore, I can adjust the above examples to the following:

HSTRLEN customer lastname

Result:

(integer) 7

This time I get the result I’m looking for. The command did what it was designed to do – return the string length of the value at the specified field of the given key.

I can verify this by checking the contents of that field:

HGET customer lastname

Result:

"Jamison"

The value has a string length of seven as expected.