skip to Main Content

I have a Node.js code. While running this code, i have an error.

This error is

ClientClosedError: The client is closed

My redis run on docker called redis1.

const redis = require("redis");
const client = redis.createClient({
    host: '127.0.0.1',
    port: 6379
});




client.on("error", error => {
    if(error) {
        console.error("ERROR***",error);
    }else {
        console.log("Redis connect.");
    }
});


//SET
client.set("user_name", "Furkan", (err, msg) => {
    if(err) {
        console.error("Error while connecting redis", err);
    }else {
        console.log(msg);
    } 
})

Furhermore; when i was run this code, dont log in terminal any code. Namely when i was run this code, didnt log in terminal ERROR*** , Redis Connected or Error while connecting redis.

These logs is exists in my code.

this link is reference for me.

How can i fix this code.?

2

Answers


  1. Chosen as BEST ANSWER

    You should add in your code before the client code or set code,

    (async () => {
       client.connect();
    })();
    

    block.


  2. you have to running redis-server on docker.

    docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search