1

I have a have set the hash using HSET function

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

and i have the following function to read the hash using HSCAN which give me an error saying TypeError: Cannot read properties of undefined (reading 'length') i hope i am missing some point on reading using HSCAN and can anyone help me please ? function to read using 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;
}

and i am expecting to get a function that reads from a redis hash using hscan with a specific key

1 Answer 1

1

here is the function to read found it in the documentation

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.