node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

"ClientClosedError: The client is closed" error in my TypeScript application redis usage

Open simaybocu opened this issue 3 months ago • 0 comments

Description

In redisUtils file, I created the following code with redis and util package

import * as redis from 'redis';
import { promisify } from 'util';

const redisClient = redis.createClient();

redisClient.on('connect', () => {
    console.log('\x1b[36m', `userapp-redis:`, '\x1b[0m', 'ready');
});

redisClient.on('error', (err) => {
    console.log('\x1b[36m', `userapp-redis:`, '\x1b[0m', 'not connected');
    console.log(`Something went wrong  ${err}`);
});

const setAsync = promisify(redisClient.set).bind(redisClient);
const expireAsync = promisify(redisClient.expire).bind(redisClient);
const getAsync = promisify(redisClient.get).bind(redisClient);
const delAsync = promisify(redisClient.del).bind(redisClient);


export const redisUtils = {
    get: getAsync,
    set: setAsync,
    expire: expireAsync,
    del: delAsync,
}; 

but as an example

import {redisUtils} from '../redis/redisUtils';
const usersData = await redisUtils.get('users');

When I use as return Promise.reject(new errors_1.ClientClosedError()); ^ ClientClosedError: The client is closed error. I want to have a file that I create for common use in this way, but what is it that I wrote that is unnecessary, wrong or incorrect? Or can you please help me with the redisUtils.ts codes in a better way?

Node.js Version

v20.11.1

Redis Server Version

6.2.1

Node Redis Version

No response

Platform

macOS

Logs

No response

simaybocu avatar Mar 20 '24 19:03 simaybocu