If you get an error that reads “WRONGTYPE Operation against a key holding the wrong kind of value” when using the LRANGE
command, it’s probably because you’re passing a key with the wrong data type.
To fix this issue, be sure that the key you pass to the LRANGE
command holds a list.
Example of Error
Here’s an example of code that causes the error:
LRANGE countries 0 -1
Result:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Here the countries
key contains a set and so we get an error.
We can use the TYPE
command to check the key’s data type:
TYPE countries
Result:
set
As suspected, the key contains a set instead of a list.
Solution
The solution is to make sure the key we pass contains the correct data type. The LRANGE
command is designed to be used with lists, so we should make sure the specified key contains a list.
Here’s an example of running the command against a key that holds a list:
LRANGE animals 0 -1
Result:
1) "Dog" 2) "Cat" 3) "Bird"
This time we didn’t get the error because the animals
key holds a list.
Let’s verify the data type with the TYPE
command:
TYPE animals
Result:
list
A list as expected.