0

I have redis installed on my system and its running as well. redis installed and running

from node application, im using below code to work with redis.

redis.js

 const redis = require("redis");
let client = redis.createClient(6379, '127.0.0.1', {});
let isRedis = false;
client.on("connect", function () {
 console.log(`connected to redis`);
 isRedis = true;
});
client.on("error", function (err) {
 console.log("redis connection error " + err);
 throw err;
});
client.on("end", function (err) {
 console.log("redis connection end " + err);
});
module.exports = {
 SetRedis,
 GetKeys,
 GetRedis,
 GetKeyRedis,
 delRedis
};

im using node index.js command to run the application which should also give me "connected to redis" when the connection is established, but i'm not getting this message on my console .

the npm package is also present in package.json

enter image description here

asked Feb 25, 2022 at 17:50

3 Answers 3

1

Node Redis 4.x doesn't allow you to pass in discrete arguments for the host and port. The canonical example of connecting to Redis with Node Redis is this:

import { createClient } from 'redis';
(async () => {
 const client = createClient();
 client.on('error', (err) => console.log('Redis Client Error', err));
 await client.connect();
 await client.set('key', 'value');
 const value = await client.get('key');
})();

If you want to connect to somewhere other than localhost on port 6379, I recommend using a URL. Like this:

createClient({ url: 'redis://awesome.redis.server:6380' });

But if you want finer control, you can find all the gritty configuration options in the documentation on GitHub.

answered Feb 25, 2022 at 19:13
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you are making mistake while making connection. It should have been

let client = redis.createClient('127.0.0.1', 6379, {});

rather than

let client = redis.createClient(6379, '127.0.0.1', {});
answered Feb 25, 2022 at 17:57

3 Comments

that too not working.
have you added client.connect(); ? As I can't see in your code snippet.
can you please let me know where to add in redis.js. may be you can make changes in redis.js which i mentioned above.
0

Working redis.js,

 const redis = require("redis");
let isRedis = false;
(async () => {
let client = redis.createClient(6379, '127.0.0.1', {});// create config
client.on("connect", function () {
 console.log(`connected to redis`);
 isRedis = true;
});
client.on("error", function (err) {
 console.log("redis connection error " + err);
 throw err;
});
client.on("end", function (err) {
 console.log("redis connection end " + err);
});
function GetKeyRedis(key) {
 return new Promise(function (resolve, reject) {
 console.log("dd----",key,isRedis);
 if (isRedis) {
 client.get(key).then((data,err) => {
 if(err){
 reject(err);
 }
 if(data){
 resolve(data)
 } else {
 resolve(false);
 }
 });
 } else {
 resolve(false);
 }
 });
}
module.exports = {
 GetKeyRedis
};
await client.connect();
})();
answered Feb 26, 2022 at 7:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.