3

I'm trying to create a Redis client. However whenever I do:

const REDIS_PORT = process.env.PORT || 6379;

const client = redis.createClient(REDIS_PORT);

I get AbortError: Ready check failed: Fatal error encountered. Command aborted. It might have been processed.

However if I do:

const REDIS_PORT = 6379;
    
const client = redis.createClient(REDIS_PORT);

It connects properly. Why do I get this error when I put process.env.PORT?

user
  • 1,022
  • 2
  • 8
  • 30
  • are you using node.js ? – roottraveller Jan 10 '21 at 15:35
  • Yes I am using nodejs. – user Jan 10 '21 at 15:36
  • Does this answer your question? [NodeJs - Using redis, connect-redis with express](https://stackoverflow.com/questions/12038128/nodejs-using-redis-connect-redis-with-express) – roottraveller Jan 10 '21 at 15:36
  • you did not say either it's a standalone or a cluster? which client are you exactly using? https://redis.io/clients#nodejs – roottraveller Jan 10 '21 at 15:38
  • It's a standalone and i'm using node_redis. Everything else works except when I include process.env.PORT, any reasons? – user Jan 10 '21 at 15:42
  • 1
    So `process.env.PORT || 6379 ` means: whatever is in the environment variable PORT, or 6379 if there's nothing there. So you pass that to `app.listen`, or to `app.set('port', ...)`, and that makes your server able to accept a "what port to listen on" parameter from the environment. – roottraveller Jan 10 '21 at 15:44

1 Answers1

0

Assign the desired value to the port key, use this code for create client

redis.createClient({port : REDIS_PORT})

you can check the documentation

const redis = require('redis');
const client = redis.createClient({
    host: '127.0.0.1',
    port: <port>
});
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39