Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 444
wenbert Avatar asked Aug 03 '11 09:08

wenbert


People also ask

Which method of the client object is used to execute a set of commands atomically using the node Redis driver?

execAsync( ) the execution of the query will happen atomically.


2 Answers

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.

like image 27
crash springfield Avatar answered Oct 16 '22 22:10

crash springfield


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);
    });
}
like image 139
stagas Avatar answered Oct 16 '22 23:10

stagas