Fix “ERR value is not an integer or out of range” When using the HRANDFIELD Command in Redis

If you’re getting an error that reads “ERR value is not an integer or out of range” when using the HRANDFIELD command in Redis, it could be because you’re passing a second argument that isn’t an integer. Or if it is an integer, it could be out of range.

Example of Error

Here’s an example of code that produces the error:

HRANDFIELD pet WITHVALUES

Result:

(error) ERR value is not an integer or out of range

Here, we get the error that I mentioned. This is because I passed two arguments, but the second one wasn’t an integer.

We can also get the error by passing a floating point number:

HRANDFIELD pet 2.2 WITHVALUES

Result:

(error) ERR value is not an integer or out of range

Solution

To solve this issue, we need to ensure that the second argument is an integer:

HRANDFIELD pet 1 WITHVALUES

Result:

1) "age"
2) "37"

This time we get the result we’re looking for.

This error could happen when you use the WITHVALUES argument, but you only want one field returned. Normally when you only want one field returned, you don’t need to pass the count argument. However, when we use the WITHVALUES argument, we must include the count argument, even if we only want one field returned.