PerduGames
PerduGames

Reputation: 1218

How do I check if a key inside a hash exists (redis)?

How do I check if a key inside a hash exists (redis)?

I tried this way:

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

however I get:

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.

I do not know how. What I'm trying is to check if my .pos key exists in my hash obj.mydict.user.toString(), how would it do it in node_redis?

Upvotes: 0

Views: 3717

Answers (3)

numan sheikh
numan sheikh

Reputation: 19

   // 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)); 

        });

Upvotes: 0

Itamar Haber
Itamar Haber

Reputation: 49942

While there is no specific command to check for field existence in a Redis Hash, you can call HGET and deduce non-existence from nil replies.

Upvotes: 2

Kevin K
Kevin K

Reputation: 2227

How about using in?

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

Upvotes: 1

Related Questions