Redis Hget对持有错误类型值的键抛出WRONGTYPE操作

问题描述

我正在使用nodeJs在redis键字段中获取一个值。像这样:

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

我正在寻找的值是一个对象,但我不断收到“针对错误类型值的键的WRONGTYPE操作”异常,如果从hget方法打印错误,我会得到:

------------------------------------
{ 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' }
------------------------------------

如果刷新redis数据库,请在redis-cli中运行HGET aaa:bbb profiles,我得到(nil)的结果,但这不应该引发此错误,或者是?

此外,如果我通过redis-cli为该键设置一个值,则可以在以后使用HGET获得它的值:

HSET sip_profiles profiles {}
(integer) 1

HGET sip_profiles profiles
"{}"

不知道为什么它可以与redis-cli一起使用,但不能与node ...甚至使用空键一起使用...

使用的节点依赖性:"redis": "^2.8.0"

解决方法

我遇到了同样的问题,我设置了一个 String 键而不是 Hash 键,然后当我尝试检索 String 键我得到了错误。

解决方案:确保所需的键是Hash类型,这样hget就不会失败

>> 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>>