【问题标题】:ClientClosedError: The client is closed error in Node.js with Docker and RedisClientClosedError:使用 Docker 和 Redis 的 Node.js 中的客户端已关闭错误
【发布时间】:2022-01-30 03:14:27
【问题描述】:

我有一个 Node.js 代码。运行此代码时,出现错误。

这个错误是

ClientClosedError:客户端已关闭

我的 redis 在名为 redis1 的 docker 上运行。

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);
    } 
})

此外;当我运行此代码时,不要登录终端任何代码。即当我运行此代码时,没有登录终端 ERROR*** , Redis ConnectedError while connection redis.

这些日志存在于我的代码中。

this link 是我的参考。

如何修复此代码?

【问题讨论】:

    标签: node.js docker redis


    【解决方案1】:

    您应该在您的代码之前添加客户端代码或设置代码,

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

    阻止。

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2014-01-20
      • 2022-01-08
      • 2022-01-20
      • 2022-08-04
      • 2023-01-30
      • 2022-01-09
      • 2020-06-07
      • 1970-01-01
      相关资源
      最近更新 更多