简体   繁体   English

无法在 Node.JS 中连接到 redis

[英]Cant connect to redis in Node.JS

Basically I have no idea why node will not connect to redis.基本上我不知道为什么节点不会连接到 redis。 I am able to connect to it via the CLI, and below you can see the brew service is started without any errors.我可以通过 CLI 连接到它,您可以在下面看到 brew 服务已启动,没有任何错误。 I've provided all the needed code and info, let me know if you need anything else.我已经提供了所有需要的代码和信息,如果您需要其他信息,请告诉我。

Node Error:节点错误:

(node:6601) UnhandledPromiseRejectionWarning: Error: The client is closed
at Commander._RedisClient_sendCommand (/Users/arialopez/code/url-shortener/node_modules/@node-redis/client/dist/lib/client/index.js:387:31)
at Commander.commandsExecutor (/Users/arialopez/code/url-shortener/node_modules/@node-redis/client/dist/lib/client/index.js:160:154)
at Commander.BaseClass.<computed> [as ping] (/Users/arialopez/code/url-shortener/node_modules/@node-redis/client/dist/lib/commander.js:8:29)
at new AsyncRedis (/Users/arialopez/code/url-shortener/redis/index.js:21:21)
at Object.<anonymous> (/Users/arialopez/code/url-shortener/lib/models.js:3:15)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
at Module.require (internal/modules/cjs/loader.js:974:19)

(Use node --trace-warnings... to show where the warning was created) (node:6601) UnhandledPromiseRejectionWarning: Unhandled promise rejection. (使用node --trace-warnings...显示警告的创建位置)(节点:6601) UnhandledPromiseRejectionWarning:未处理的 promise 拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with.catch().此错误源于在没有 catch 块的情况下抛出异步 function 内部,或拒绝未使用.catch() 处理的 promise。 To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode ).要终止未处理的 promise 拒绝的节点进程,请使用 CLI 标志--unhandled-rejections=strict (请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode )。 (rejection id: 2) (node:6601) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (拒绝 ID:2)(节点:6601)[DEP0018] DeprecationWarning:不推荐使用未处理的 promise 拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.将来,未处理的 promise 拒绝将使用非零退出代码终止 Node.js 进程。

Connection Code:连接代码:

require('dotenv').config();
const redis = require('redis');
const { promisify } = require('util');

const defaultOptions = { // The defaults are being used not env.
    host: process.env.REDIS_HOST || '127.0.0.1',
    port: process.env.REDIS_PORT || 6379,
    keyPrefix: process.env.REDIS_PREFIX || 'url-'
};

class AsyncRedis {
    constructor() {
        this.client = redis.createClient(defaultOptions);
        this.client.on('error', this.error); // error handler defind below
        this.client.ping() // WHERE CODE IS BREAKING
    }
    ... Class code
}

Brew Status:酿造状态:

redis             started

You should call client.connect() before executing commands.您应该在执行命令之前调用client.connect() BTW, redis@4.0.0 have built-in promises support, there is no need to create an async wrapper顺便说一句, redis@4.0.0有内置的 Promise 支持,不需要创建异步包装器

const redis = require('redis'),
  client = redis.createClient({
    // ...
  });

client.on('error', err => {
  // ...
});

await client.connect();
console.log(await client.ping());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM