Fix “WRONGTYPE Operation against a key holding the wrong kind of value” when using ZSCORE in Redis

If you get an error that reads “WRONGTYPE Operation against a key holding the wrong kind of value” when using the ZSCORE command in Redis, it’s because you’re passing a key with the wrong data type.

To fix this issue, make sure that you pass a sorted set to the ZSCORE command.

Example of Error

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

ZSCORE animals Bite

Result:

(error) WRONGTYPE Operation against a key holding the wrong kind of value

In my case, the animals key holds a set (not a sorted set), which is why I got the error.

We can use the TYPE command to check a key’s type:

TYPE animals

Result:

set

As suspected, the key holds a set, which is the wrong data type for the ZSCORE command.

Solution

The solution is to make sure the key we pass to the ZSCORE command holds a sorted set.

Let’s replace the cats key with another key that holds a sorted set:

ZSCORE cats "Meow"

Result:

"15"

This time the score for the specified member was returned as expected. That’s because the cats key holds a sorted set. In this case the member is Meow and its score is 15.

Let’s check the key’s data type:

TYPE cats

Result:

zset

As expected, it’s a sorted set.