Reputation: 2853
I am trying to increment a value for a key-value in redis, if the value already exists in redis. For instance if we have
client.get(key, function checkRedis(err, data){
var redisData = JSON.parse(data);
if(redisData === null){
//do something
}else{
client.incr(redisData.val);
}
});
From my understanding, according to the documentation using "incr" should automatically increment that specific value by 1. But i am unable to see this happen successfuly, am i missing something
Upvotes: 3
Views: 11600
Reputation: 545
You need to use the INCR key to increment value in redis
const redisClient = require('ioredis');
const name = 'Bob';
redisClient.incr('id', (err, id) =>
{
redisClient.hmset('user:' + id, 'username', name);
});
Upvotes: 0
Reputation: 1261
You need to give client
the key
not the value.
I believe the below will do what you need.
client.get(key, function checkRedis(err, data){
var redisData = JSON.parse(data);
if(redisData === null){
//do something
}else{
redisData.val++;
client.set(key, redisData);
}
});
Upvotes: 3