Я использую Redis со своим приложением NodeJS. Я хочу сохранить массив объектов (политик) по ключу как ha sh в Redis. Ниже мой ключ и значение: -
Key - <tenantid>~<userid> e.g. - tenant123~john123
Значение - [{ ptype: 'p', v0: 'admin', v1: '/*', v2: 'GET' }, { ptype: 'p', v0: 'viewer', v1: '/post', v2: 'GET' }]
I ' m получает ошибку ниже: -
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>}
Ниже мой код NodeJS, в котором я вызываю hmset (), чтобы сохранить политики в 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();
});
});
}
Я пытался отладить много но не мог это исправить. Разве в 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, 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();
});
});
}
Ошибка: -
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)"