Node.js client.get()的值为;“真的”;而不是真正的价值

Node.js client.get()的值为;“真的”;而不是真正的价值,node.js,redis,nowjs-sockets,Node.js,Redis,Nowjs Sockets,我正在使用nowjs和node_redis。我试图创造一些非常简单的东西。但到目前为止,本教程没有给我留下任何内容,因为它们只做console.log() 在我的客户端: now.receiveShowRedisCard = function(card_id) { alert("redis card: "+card_id); } 警报只给出“true”-我希望得到钥匙“card”的值,即“apple” 有什么想法吗?您正在尝试以同步方式使用异步库。这是正确的方法: //REDIS va

我正在使用nowjs和node_redis。我试图创造一些非常简单的东西。但到目前为止,本教程没有给我留下任何内容,因为它们只做console.log()

在我的客户端:

now.receiveShowRedisCard = function(card_id) {
    alert("redis card: "+card_id);
}
警报只给出“true”-我希望得到钥匙“card”的值,即“apple”


有什么想法吗?

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

//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);
    });
}
使用异步Redis
npm i异步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");

一种选择是使用Bluebird将Redis回调转化为承诺。然后您可以将其与
.Then()
异步/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

是否可以将客户端导出到另一个模块以供使用?我正在尝试导出它,但它仍然返回
true
进行查询。
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")