skip to Main Content

I’m using Redis with my NodeJS app. I want to store an array of objects (policies) against a key as a hash in Redis. Below is my key and value:-

Key – <tenantid>~<userid> e.g. - tenant123~john123

Value – [{ ptype: 'p', v0: 'admin', v1: '/*', v2: 'GET' }, { ptype: 'p', v0: 'viewer', v1: '/post', v2: 'GET' }]

I’m getting below error :-

err
ReplyError: ERR wrong number of arguments for 'hmset' command
args:Array(2) ["tenant123~john123", "[{ ptype: 'p', v0: 'admin', v1: '/*', v2: 'GET' }, { ptype: 'p', v0: 'viewer', v1: '/post', v2: 'GET' }]"]
code:"ERR"
command:"HMSET"
message:"ERR wrong number of arguments for 'hmset' command"
name:"ReplyError"
stack:"ReplyError: ERR wrong number of arguments for 'hmset' command
    at parseError (/var/task/node_modules/redis-parser/lib/parser.js:179:12)
    at parseType (/var/task/node_modules/redis-parser/lib/parser.js:302:14)"
__proto__:RedisError {constructor: , name: <accessor>}

Below is my NodeJS code where I’m calling hmset() to save the policies into Redis.

module.exports.updateCache = async (hashKey, ttl, obj, logger) => {
    logger.debug('Redis:: Key:', hashKey, ', Obj:', obj);
    return new Promise(function (resolve, reject) {
        myRedisClient.hmset(hashKey, obj, (err, result) => {
            if (err) {
                logger.error(err);
                reject(err);
            } else {
                myRedisClient.expire(hashKey, ttl);
                console.log('result:', result);
                resolve(true);
            }
            console.log('closing redis connection');
            myRedisClient.quit();
        });
    });
}

I tried to debug a lot but could not fix this. Is it not possible to store an array of objects against a hashkey in Redis?

Please assist. Thanks

[Update]

New code:-

module.exports.updateCache = async (hashKey, ttl, obj, logger) => {
    logger.debug('Redis:: Key:', hashKey, ', Obj:', obj);
    return new Promise(function (resolve, reject) {
        myRedisClient.hmset(hashKey, JSON.stringify(obj), (err, result) => {
            if (err) {
                logger.error(err);
                reject(err);
            } else {
                myRedisClient.expire(hashKey, ttl);
                console.log('result:', result);
                resolve(true);
            }
            console.log('closing redis connection');
            myRedisClient.quit();
        });
    });
}

Error:-

err
ReplyError: ERR wrong number of arguments for 'hmset' command
args:Array(2) ["tenant123~john123", "[{"ptype":"p","v0":"admin","v1":"/*","v2":"GET"}]"]
code:"ERR"
command:"HMSET"
message:"ERR wrong number of arguments for 'hmset' command"
name:"ReplyError"
stack:"ReplyError: ERR wrong number of arguments for 'hmset' command
    at parseError (/var/task/node_modules/redis-parser/lib/parser.js:179:12)
    at parseType (/var/task/node_modules/redis-parser/lib/parser.js:302:14)"

3

Answers


  1. It is impossible.

    Redis stores everything in string or in its string representation. you can’t store array of objects or even object(except string) in redis.

    There is no direct means to store objects – it can be done only via serialization and storing resultant byte-array.

    Login or Signup to reply.
  2. Redis store strings so you must serialize your JSON object/array to store them, and then deserialize them when you read those keys.

    module.exports.updateCache = async (hashKey, ttl, obj, logger) => {
        logger.debug('Redis:: Key:', hashKey, ', Obj:', obj);
        return new Promise(function (resolve, reject) {
            myRedisClient.hmset(hashKey, { array: JSON.stringify(obj) }, (err, result) => {
                if (err) {
                    logger.error(err);
                    reject(err);
                } else {
                    myRedisClient.expire(hashKey, ttl);
                    console.log('result:', result);
                    resolve(true);
                }
                console.log('closing redis connection');
                myRedisClient.quit();
            });
        });
    }
    
    Login or Signup to reply.
  3. The function hmset is deprecated as of Redis v4.0. Use hset instead.

    Using your newer example, something like

    myRedisClient.hset(hashKey, , 'name', JSON.stringify(obj), (err, result) => ...
    

    could work. Failures. What was failing, the cause of the error? Pay close attention to the keyname – if this is calling a key by the same name but is a different datatype such as a set, this call would fail.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search