来自 client.get() 的值为“true”而不是真正的价值
我正在使用 nowjs 和 node_redis。我正在尝试创建一些非常简单的东西。但到目前为止,该教程让我空白,因为它们只执行 console.log()。
//REDIS
var redis = require("redis"),
client = redis.createClient();
client.on("error", function (err) {
console.log("Error "+ err);
});
client.set("card", "apple");
everyone.now.signalShowRedisCard = function() {
nowjs.getGroup(this.now.room).now.receiveShowRedisCard(client.get("card").toString());
}
在我的客户端:
now.receiveShowRedisCard = function(card_id) {
alert("redis card: "+card_id);
}
警报仅给出“true” - 我期望获得关键“card”的值,即“apple”。
有什么想法吗?
I am using nowjs and node_redis. I am trying to create something very simple. But so far, the tutorial have left me blank because they only do console.log().
//REDIS
var redis = require("redis"),
client = redis.createClient();
client.on("error", function (err) {
console.log("Error "+ err);
});
client.set("card", "apple");
everyone.now.signalShowRedisCard = function() {
nowjs.getGroup(this.now.room).now.receiveShowRedisCard(client.get("card").toString());
}
In my client side:
now.receiveShowRedisCard = function(card_id) {
alert("redis card: "+card_id);
}
The alert only gives out "true" - I was expecting to get the value of the key "card" which is "apple".
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在尝试以同步方式使用异步库。这是正确的方法:
You are trying to use an async library in a sync way. This is the right way:
一种选择是使用 Bluebird 将 Redis 回调转换为 Promise。然后您可以将其与
.then()
或async/await
一起使用。请注意,您的方法应该附加
Async
。One option is to use Bluebird to turn Redis callbacks into promises. Then you can use it with
.then()
orasync/await
.Notice your methods should have
Async
appened to them.使用异步 Redis
npm i async-redis --save
Use Async Redis
npm i async-redis --save
还可以使用node_redis库提供的函数
const getAsync = promisify(client.get).bind(client);
并使用它从 redis 获取值,如下所示
const value = wait getAsync(key)
You can also use a function provided by node_redis library
const getAsync = promisify(client.get).bind(client);
and use this to get values from redis as follows
const value = await getAsync(key)