skip to Main Content

The error says Redis method "SETEX" undefined
and application deployed into Heroku cloud

Here is the Node-Redis setup code, please Have a look

import { createClient } from 'redis';
import { REDIS_URI } from '../config';

export let redisClient;

(async () => {
    try {
        redisClient = createClient({ url: REDIS_URI });
        // redisClient.on("connect", () => { 
        // })
        redisClient.on("error", (err) => console.log("Redis Client Connection Error"));

        console.log("Redis cashe app database connected...");
        await redisClient.connect();
        // if (NODE_ENV) {
        // } else {
        //     redisClient = createClient({
        //         url: REDIS_URI});
        //     redisClient.on("error", (err) => console.log("Redis Client Connection Error", err));

        //     console.log("Redis cashe app database connected...");
        //     await redisClient.connect();
        // }
    } catch (error) {
        console.log('Redis app connection Error...', error);
    }
})();

Whenever I need redis to get or set the data I just called "redisClient" as a redis instance because it’s an exported(I’m using ES6) instance of redis.

Here is the Full error

2022-09-14T14:59:46.099027+00:00 app[web.1]: /app/dist/controller/contact/index.js:212
2022-09-14T14:59:46.099044+00:00 app[web.1]:                         return _redisClient.redisClient.SETEX(key, _config.REDIS_DEF_EXP_TIME, JSON.stringify(contacts));
2022-09-14T14:59:46.099045+00:00 app[web.1]:                                                         ^
2022-09-14T14:59:46.099045+00:00 app[web.1]: 
2022-09-14T14:59:46.099046+00:00 app[web.1]: TypeError: Cannot read properties of undefined (reading 'SETEX')
2022-09-14T14:59:46.099046+00:00 app[web.1]:     at _callee5$ (/app/dist/controller/contact/index.js:212:57)
2022-09-14T14:59:46.099046+00:00 app[web.1]:     at tryCatch (/app/node_modules/regenerator-runtime/runtime.js:63:40)
2022-09-14T14:59:46.099047+00:00 app[web.1]:     at Generator.invoke [as _invoke] (/app/node_modules/regenerator-runtime/runtime.js:294:22)
2022-09-14T14:59:46.099047+00:00 app[web.1]:     at Generator.next (/app/node_modules/regenerator-runtime/runtime.js:119:21)
2022-09-14T14:59:46.099048+00:00 app[web.1]:     at asyncGeneratorStep (/app/dist/controller/contact/index.js:22:103)
2022-09-14T14:59:46.099048+00:00 app[web.1]:     at _next (/app/dist/controller/contact/index.js:24:194)
2022-09-14T14:59:46.099048+00:00 app[web.1]:     at /app/dist/controller/contact/index.js:24:364
2022-09-14T14:59:46.099048+00:00 app[web.1]:     at new Promise (<anonymous>)
2022-09-14T14:59:46.099049+00:00 app[web.1]:     at /app/dist/controller/contact/index.js:24:97
2022-09-14T14:59:46.099049+00:00 app[web.1]:     at /app/dist/controller/contact/index.js:226:30
2022-09-14T14:59:46.099049+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/model.js:5074:18
2022-09-14T14:59:46.099050+00:00 app[web.1]:     at processTicksAndRejections (node:internal/process/task_queues:78:11)
 -->

And this is the OS Node environment setup

**Environment:**
 - **Node.js Version**: 16.x
 - **Redis Server Version**: <!-- e.g. "redis-server --version" -->
 - **Node Redis Version**: 4.0.2
 -  - **Platform**:  Heroku Cloud OS

I’m getting the "SETEX" method undefined error, and the system isn’t working anymore.

Thanks

2

Answers


  1. Hey Internazionaleauto!

    I suspect this is happening because of how you are exporting the Redis client.
    If you change your initialization code to something like the following:

    async function initRedisClient() {
        try {
            const client = createClient({ url: REDIS_URI });
            client.on("error", (err) => console.log("Redis Client Connection Error"));
            await client.connect();
            console.log("Redis cashe app database connected...");
            return client;
        } catch (error) {
            console.log('Redis app connection Error...', error);
        }
    }
    
    export { initRedisClient };
    

    which you would now initialize in another file like so:

    import { initRedisClient } from './initRedisClient'
    
    // later in the file, in an async function body
    
    const redisClient = await initRedisClient();
    
    

    I suspect the original error should be fixed.

    The error says Redis method "SETEX" undefined and application deployed into Heroku cloud

    This is because the code importing your original export let redisClient; does not wait for it to finish initializing, which means that it imports redisClient while it’s still undefined and then tries to call methods on it.

    Login or Signup to reply.
  2. I solve problem

    const redisClient = redis.createClient({
        socket:{
            host:process.env.REDIS_HOST,
            port:process.env.REDIS_PORT,
            tls:true,
    
        },
        password:process.env.REDIS_PASSWORD,
    
    })
    
    This section is saved me
    tls : true
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search