【问题标题】:How do I check if a key inside a hash exists (redis)?如何检查哈希中的键是否存在(redis)?
【发布时间】:2017-10-18 17:38:41
【问题描述】:

如何检查哈希中的键是否存在(redis)?

我试过这样:

redisClient.exists(obj.mydict.user.toString().pos, function(err,reply) {
                if(!err) {
                    if(reply !== null) {
                    ...

但是我得到了:

node_redis: Deprecated: The EXISTS command contains a "undefined" argument.
This is converted to a "undefined" string now and will return an error from v.3.0 on.
Please handle this in your code to make sure everything works as you intended it to.

我不知道怎么做。我正在尝试检查我的 .pos 密钥是否存在于我的哈希 obj.mydict.user.toString() 中,它会如何在 node_redis?

【问题讨论】:

    标签: javascript redis node-redis


    【解决方案1】:

    虽然没有特定的命令来检查 Redis 哈希中的字段是否存在,但您可以调用 HGET 并从 nil 回复中推断不存在。

    【讨论】:

      【解决方案2】:

      使用in怎么样?

      var hash = {'a': 1, 'b': 2};
      console.log('a' in hash);
      console.log('c' in hash);
      

      【讨论】:

      • 所以,我就是这样做的,但是我必须在redis中搜索所有hash,我只想检查银行中是否有密钥。
      • 从我在这里的文档中看到的,它似乎没有。所以方法是:redisClient.hgetall(obj.mydict.user.toString(),function(err,reply) { if(!err) {if("pos" in reply){ ...
      【解决方案3】:
         // you can use 'hget' to check the existence of your key in particular hash like as follow:
      
      client.hget('HASH NAME', 'mykey',function(err, result) 
              {
                   if(err)
                    console.log(err.message);
                   console.log(JSON.stringify(result)); 
      
              });
      

      【讨论】:

        猜你喜欢
        • 2011-05-30
        • 2012-04-05
        • 2023-04-07
        • 2011-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        相关资源
        最近更新 更多