来自 client.get() 的值为“true”而不是真正的价值

发布于 2024-11-27 16:19:07 字数 620 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑脸一如从前 2024-12-04 16:19:07

您正在尝试以同步方式使用异步库。这是正确的方法:

//REDIS
var redis = require("redis"),
    client = redis.createClient();

client.on("error", function (err) {
    console.log("Error "+ err);
});

client.set("card", "apple", function(err) {
    if (err) throw err;
});

everyone.now.signalShowRedisCard = function() {
    var self = this;
    client.get("card", function (err, res) {
        nowjs.getGroup(self.now.room).now.receiveShowRedisCard(res);
    });
}

You are trying to use an async library in a sync way. This is the right way:

//REDIS
var redis = require("redis"),
    client = redis.createClient();

client.on("error", function (err) {
    console.log("Error "+ err);
});

client.set("card", "apple", function(err) {
    if (err) throw err;
});

everyone.now.signalShowRedisCard = function() {
    var self = this;
    client.get("card", function (err, res) {
        nowjs.getGroup(self.now.room).now.receiveShowRedisCard(res);
    });
}
我一向站在原地 2024-12-04 16:19:07

一种选择是使用 Bluebird 将 Redis 回调转换为 Promise。然后您可以将其与 .then()async/await 一起使用。

import redis from 'redis'
import bluebird from 'bluebird'

bluebird.promisifyAll(redis)
const client = redis.createClient()

await client.set("myKey", "my value")
const value = await client.getAsync("myKey")

请注意,您的方法应该附加Async

One option is to use Bluebird to turn Redis callbacks into promises. Then you can use it with .then() or async/await.

import redis from 'redis'
import bluebird from 'bluebird'

bluebird.promisifyAll(redis)
const client = redis.createClient()

await client.set("myKey", "my value")
const value = await client.getAsync("myKey")

Notice your methods should have Async appened to them.

风透绣罗衣 2024-12-04 16:19:07

使用异步 Redis

npm i async-redis --save

const asyncRedis = require("async-redis");    
const client = asyncRedis.createClient(); 

await client.set("string key", "string val");
const value = await client.get("string key");

console.log(value);

await client.flushall("string key");

Use Async Redis

npm i async-redis --save

const asyncRedis = require("async-redis");    
const client = asyncRedis.createClient(); 

await client.set("string key", "string val");
const value = await client.get("string key");

console.log(value);

await client.flushall("string key");
两个我 2024-12-04 16:19:07

还可以使用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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文