'NodeJS can not connect to Redis

I have nodejs app which needs to connect to Redis database. However, when trying to connect to Redis it always returns "false". I have running Redis instance on the same machine which is accessible from cli, also have another nodejs app which is connected to this same instance of Redis.

I don'g see anything wrong in /var/log/redis/redis-server.log nor printed in the console.

Any ideas what might be the issue?

$ nodejs -v v8.11.1

$redis-server -v Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=1bc80a08306a3efd

const redis = require('redis'),
redisClient = redis.createClient(6379, '127.0.0.1');

const REDIS_USER_DATA_INDEX = 2;

redisClient.select(REDIS_USER_DATA_INDEX);

redisClient.on('connect', function () {
    console.log('redis connected');
}).on('error', function (error) {
    console.log(error);
});

console.log(`connected ${redisClient.connected}`);

EDIT I'm not getting any events in both redisClient.on() callbacks



Solution 1:[1]

Basically, you're calling redisClient.on(), which is an Async function, and you're outputting the connected status outside the callback function which definitely returns false since redis is still not connected. I would suggest put your code within the callback function. Try the code given below:

    const redis = require('redis'),
redisClient = redis.createClient(6379, '127.0.0.1');

const REDIS_USER_DATA_INDEX = 2;

redisClient.select(REDIS_USER_DATA_INDEX);

redisClient.on('connect', function () {
    console.log('redis connected');
    console.log(`connected ${redisClient.connected}`);
}).on('error', function (error) {
    console.log(error);
});

Let me know if you've any question. Thanks

Solution 2:[2]

redis' connect() function is an async function. The promise has to resolve for you to be able to use the connection.

The code below should work:

const redis = require("redis");

let redisPort = 6379;  // Replace with your redis port
let redisHost = "127.0.0.1";  // Replace with your redis host
const client = redis.createClient({
    socket: {
      port: redisPort,
      host: redisHost,
    }
  });

(async () => {
    // Connect to redis server
    await client.connect();
})();


console.log("Attempting to connect to redis");
client.on('connect', () => {
    console.log('Connected!');
});

// Log any error that may occur to the console
client.on("error", (err) => {
    console.log(`Error:${err}`);
});

// Close the connection when there is an interrupt sent from keyboard
process.on('SIGINT', () => {
    client.quit();
    console.log('redis client quit');
});

Solution 3:[3]

Maybe it's because when you are trying to display redisClient.connected, you are not connected yet (Javascript is Asynchrone)

I think you should log this directly inside your event like this :

redisClient.on('connect', function () {
    console.log('redis connected');
    console.log(`connected ${redisClient.connected}`);
}).on('error', function (error) {
    console.log(error);
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Harsh Srivastava
Solution 2
Solution 3 Sparw