Redis Hget throwing WRONGTYPE Operation against a key holding the wrong kind of value

6.8k views Asked by At

I'm using nodeJs to obtain a value in a redis key field. Something like this:

let result = await redisM.hget('aaa:bbb', 'profiles');

The value I'm looking for is an object but I keep getting "WRONGTYPE Operation against a key holding the wrong kind of value" exception and I if print the error from the hget method I get:

------------------------------------
{ ReplyError: WRONGTYPE Operation against a key holding the wrong kind of value
    at parseError (/PATH123/node_modules/redis-parser/lib/parser.js:193:12)
    at parseType (/PATH123/node_modules/redis-parser/lib/parser.js:303:14)
  command: 'HGET',
  args: [ 'aaa:bbb', 'profiles' ],
  code: 'WRONGTYPE' }
------------------------------------

If I flush redis db, run the HGET aaa:bbb profiles in redis-cli I get (nil) result but that is not supposed to be throwing this error, or is it?

Also, if I set a value to that key via redis-cli I can get it's value using HGET afterwards:

HSET sip_profiles profiles {}
(integer) 1

HGET sip_profiles profiles
"{}"

Don't understand why it works with redis-cli but not with node... even with an empty key...

Node dependency used: "redis": "^2.8.0"

1

There are 1 answers

0
G. I. Joe On

I got the same problem, I set up a String Key instead of a Hash key, then when I tried to retrieve the keys of the String key I got the error.

Solution: Ensure the desired key is Hash type, then the hget is not gonna fail

>> redis.set 1,1
=> "OK"
>> redis.hkeys 1
Operation against a key holding the wrong kind of value: Expected Hash at 1.
Redis::CommandError: WRONGTYPE Operation against a key holding the wrong kind of value
from /onelogin/src/vendor/bundle/gems/fakeredis-0.5.0/lib/redis/connection/memory.rb:991:in `data_type_check'
from /onelogin/src/vendor/bundle/gems/fakeredis-0.5.0/lib/redis/connection/memory.rb:213:in `hkeys'
from /onelogin/src/vendor/bundle/gems/fakeredis-0.5.0/lib/fakeredis/command_executor.rb:10:in `send'
from /onelogin/src/vendor/bundle/gems/fakeredis-0.5.0/lib/fakeredis/command_executor.rb:10:in `write'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:257:in `write'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:236:in `io'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:255:in `write'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:214:in `process'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:208:in `each'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:208:in `process'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:353:in `ensure_connected'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:207:in `process'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:292:in `logging'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:206:in `process'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:112:in `call'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis.rb:1995:in `hkeys'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis.rb:37:in `synchronize'
from /usr/local/lib/ruby/1.8/monitor.rb:242:in `mon_synchronize'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis.rb:37:in `synchronize'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis.rb:1994:in `hkeys'
from (irb):36>>