Rajesh Barik
Rajesh Barik

Reputation: 191

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;
}

Upvotes: 4

Views: 13941

Answers (1)

Faizuddin Mohammed
Faizuddin Mohammed

Reputation: 4328

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.

Upvotes: 10

Related Questions