Value from client.get() is "true" instead of the real value
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");
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);
});
}
You can also use a function provided by node_redis libraryconst getAsync = promisify(client.get).bind(client);
and use this to get values from redis as followsconst value = await getAsync(key)
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.