简体   繁体   English

为什么回调不适用于节点上的 Redis

[英]Why doesn't callback work for Redis on node

The following code works on redis version 3.1.2, but when I upgrade the redis libary to 4.1.0, the call back stops working.以下代码适用于 redis 版本 3.1.2,但是当我将 redis 库升级到 4.1.0 时,回调停止工作。 There is a REDIS server running on localhost with no password.有一个 REDIS 服务器在 localhost 上运行,没有密码。 I am using node 16 LTS.我正在使用节点 16 LTS。

'use strict'
import {createClient} from 'redis';
const redisClient = createClient();

redisClient.on('connect', function() {
    console.log('you are now connected');
});
redisClient.on('ready', function() {
    console.log('you are now ready');
});
redisClient.on('error', function(err) {
    console.log(`redis error: ${err}`)
});

// added for 4.1.0 because there is no autoconnect.
await redisClient.connect();

await redisClient.set("test","value", function(err, reply) {
    console.log('set complete');
});

When I run this on Redis client 3.1.2, I get "set complete" after set is call.当我在 Redis 客户端 3.1.2 上运行它时,我在调用设置后得到“设置完成”。 On Redis client 4.1.0, I get nothing as if the callback isn't called.在 Redis 客户端 4.1.0 上,我什么也没得到,就好像没有调用回调一样。 The value is set in the REDIS database even if the callback doesn't trigger.即使回调未触发,该值也会在 REDIS 数据库中设置。 I verified the value using redis cli.我使用 redis cli 验证了该值。 There are no error messages.没有错误消息。 I do get the connect and ready event message.我确实收到了连接和就绪事件消息。

What am I missing here?我在这里错过了什么? I am pretty sure it's something simple that I am overlooking.我很确定这是我忽略的简单事情。

Node Redis 4.x, following semver conventions, introduced many breaking changes.节点 Redis 4.x 遵循 semver 约定,引入了许多重大更改。 One of them is that it uses promises instead of callbacks.其中之一是它使用承诺而不是回调。 You can read all the details at https://github.com/redis/node-redis .您可以在https://github.com/redis/node-redis阅读所有详细信息。

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

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