Im running a function to ingest data to a Redis instance. However, I am encountering an issue where I can’t seem to connect to the client.
Here is the part of the code where it connects to the server.
const redis = require('redis');
require('dotenv').config;
const REDISHOST = process.env.REDISHOST;
const REDISPORT = 6379;
const REDISAUTH = process.env.AUTHSTRING;
const client = redis.createClient({
port: REDISPORT,
host: REDISHOST,
password: REDISAUTH
});
await client.connect();
Here is the whole error message:
"Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)
at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)"
Any help would be appreciated. Thanks
I have tried using ioredis instead of redis but it shows a different error altogether.
2
Answers
It’s trying to connect to localhost and Redis doesn’t exist there. IF not given a hostname, this is the default behavior of Redis.
Looking at your code, this is probably because the REDISHOST environment variable isn’t defined.
The suggested solution to the problem is to modify the code to use a URL-based connection string instead of explicitly specifying the Redis server configuration parameters. The new connection string should be in the format of:
Change:
To:
That worked for me.