我已经使用 HSET 函数设置了哈希值
async (userObject) => {
const { phone } = userObject;
const key = keyGenerator.getUserAuthKey(phone);
try {
for (const [field,value] of Object.entries(userObject)) {
await redisClient.HSET(key,field,value);
}
logger.info(`------ user data cached in redis successfully`);
return true;
} catch (error) {
logger.error(`Error in saving user data in redis : ${error.message}`);
throw error
}
}
我有以下函数来使用 HSCAN 读取哈希值,这给了我一个错误,说
TypeError: Cannot read properties of undefined (reading 'length')
我希望我在使用 HSCAN 读取时遗漏了一些要点,有人可以帮助我吗?使用 hscan 读取的函数
async function readRedisHashUsingHScan(hashKey) {
let cursor = '0';
const result = {};
do {
const reply = await redisClient.hScan(hashKey, cursor);
const elements = reply[1];
cursor = reply.cursor;
for (let i = 0; i < elements.length; i += 2) {
result[elements[i]] = elements[i + 1];
}
} while (cursor !== '0');
return result;
}
我希望得到一个使用 hscan 和特定键从 Redis 哈希读取的函数
这是在文档中找到的要阅读的函数
async function scanHash(hashKey) {
const result = {};
try {
// Iterate over fields and values in the hash
for await (const { field, value } of redisClient.hScanIterator(hashKey)) {
result[field] = value;
}
} catch (error) {
console.error('Error reading Redis hash:', error);
throw error;
}
return result;
}