The value from client.get () is "true" instead of real value

I am using nowjs and node_redis. I am trying to create something very simple. But so far the tutorial has left me empty because they are only 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());
}

On my client side:

now.receiveShowRedisCard = function(card_id) {
    alert("redis card: "+card_id);
}

The warning displays only "true" - I expected to get the value of the key "card", which is an "apple".

Any ideas?

+9
source share
3 answers

You are trying to use the asynchronous library synchronously. This is the correct 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);
    });
}
+12
source

Use Async Redis

npm 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");
+1
source

Bluebird Redis . .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.

0

All Articles