node.js - Redis 中的 typescript 错误 - 事件

标签 node.js typescript redis

import * as redis from 'redis'
import config from '../../config/config'
const client = redis.createClient(config.redis.port, host, config.redis.options);
client.on('ready', () => {
  console.log('redis is ready.')
});

在 typescript 中出现以下错误

[ts] 属性 'on' 不存在于类型 '{ address: any; connection_options: { [x: string]: 任何; }; connection_id:数字;连接: bool ...'。任何

最佳答案

首先你是否安装了redis模块的type模块?我强烈建议您这样做,因为它可以帮助 TypeScript 编译器并可能帮助您的 IDE 了解 redis 模块中可用的命令和接口(interface)。为此:

npm i --save-dev @types/redis

其次,尝试添加 .on('error', console.error) 以查看您的客户端是否发出任何有意义的连接错误。您的客户端凭据可能存在问题,导致连接失败。

最后,您可以完全跳过 .on('ready', ...) 事件处理程序,因为它不是必需的。 redis模块会在连接建立之前将所有发送的命令排队,连接成功后会自动发送。您可以使用简单的 setget 命令开始测试客户端。

关于node.js - Redis 中的 typescript 错误 - 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49113524/

相关文章:

facebook - 获取 PSID : Angular2 and Facebook messenger extension

javascript - Commander.js 简单用例 : one single file argument

具有自定义集群配置的 Redis 集群

Redis 管理面板

javascript - 如何为 Sequelize 模型设置外键?

javascript - 只向前端发送指定的javascript文件

javascript - expressjs POST header 参数?

php - Codeigniter 在 nodejs 中读取 session cookie

html - 为列表中的任何特定元素设置背景

php - 排队 mysql 查询的最佳方法是什么?在速度方面