Tuesday, 16 May 2017

How to delete on based of subkey using hset in redis in node js app

I am trying to use redis in my nodejs application. I am able to save and retrieve value perfectly. I am using hash which allows me to have single unique key as well sub-keys. Here offer id is my sub key and offer is key.

I am saving offer like below

let offer = {
    offerId: req.query.id,
    offerName: req.query.offerName,
    offerVendor: req.query.offerVendor
}
client.hset('offer', offer.offerId, JSON.stringify(offer), redis.print);

and retrieving value like below

try {   
    console.log('ping response:', await client.ping());
    const offerId = req.query.id;
    client.hget('offer', offerId, function(err, reply){
        let offer = JSON.parse(reply);
        res.send(offer);
    });
} catch(err) {
    console.log('ping error:', err);
}

I want to delete offer based on id as like saving or getting on based of id. I have gone through documentation https://github.com/NodeRedis/node_redis#usage-example but didn't find there.

What is the way to do delete offer based on id ?



via Williams

No comments:

Post a Comment