Using redis and node.js problem: why does redis.get return false?

I am running a simple web application supported by node.js and I am trying to use redis to store some key-value pairs. All I do is run β€œnode index.js” on the command line, and here are the first few lines of my index.js:

var app = require('express').createServer(); var io = require('socket.io').listen(app); var redis = require('redis'); var redis_client = redis.createClient(); redis_client.set("hello", "world"); console.log(redis_client.get("hello")); 

However, everything I get for redis_client.get("hello") instead of "world" is false . Why doesn't he return "world" ?

(And I start the redis server)

What is strange is that the example code shown here works fine and produces the expected result. Is there something I'm doing wrong for simple set and get ?

+4
source share
2 answers

I could put get asynchronously, so you get the value by a callback.

Edit:
This is really asynchronous: file.js

+7
source

Here you go! For ES6

 redis_client.get("hello", (err, data)=>{ if(err){ throw err; } console.log(data); }); 

For ES5

 redis_client.get("hello", function(err, data){ if(err){ throw err; } console.log(data); }); 
+1
source

All Articles