Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How fix arguments error of HSET in Redis?

I am implementing a cacheing layer in NodeJS and MongoDB using Redis. I am fairly new to Redis. So I am having trouble where I am trying to automatically clear cache after a given timing. The error I am getting

ReplyError: ERR wrong number of arguments for 'hset' command

This is my code block

mongoose.Query.prototype.exec = async function() {

  const key = JSON.stringify(
      Object.assign({}, this.getQuery(), {collection: 
      this.mongooseCollection.name})
  );
  const cachedValue = await client.hget(this.hashKey, key);

  if(cachedValue) {
      const parsedDoc = JSON.parse(cachedValue);

      return Array.isArray(parsedDoc) ? parsedDoc.map(doc => new 
      this.model(doc)) : new this.model(parsedDoc);
  }

  const result = await exec.apply(this, arguments);

  client.hset(this.hashKey, key, JSON.stringify(result), 'EX', 10);

  return result;
}
like image 259
Rajesh Barik Avatar asked Oct 15 '25 15:10

Rajesh Barik


1 Answers

Redis HSET only accepts 3 arguments. If you want to store multiple keys in one call, you should use HMSET.

Reference:

https://redis.io/commands/hset

https://redis.io/commands/hmset

client.hmset(this.hashKey, key, JSON.stringify(result), 'EX', 10);

should work.

like image 150
Faizuddin Mohammed Avatar answered Oct 18 '25 05:10

Faizuddin Mohammed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!