【发布时间】:2020-06-18 08:52:26
【问题描述】:
我将 Redis 与我的 NodeJS 应用程序一起使用。我想在 Redis 中将一组对象(策略)作为哈希存储在一个键上。以下是我的关键和价值:-
密钥- <tenantid>~<userid> e.g. - tenant123~john123
价值 - [{ ptype: 'p', v0: 'admin', v1: '/*', v2: 'GET' }, { ptype: 'p', v0: 'viewer', v1: '/post', v2: 'GET' }]
我遇到以下错误:-
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 中是否不能针对 hashkey 存储对象数组?
请帮忙。谢谢
[更新]
新代码:-
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)"
【问题讨论】:
标签: node.js redis node-redis