2

I can store a key/value in Redis but I can't retrieve the value.

store.js

var client = redis.createClient();

token ='ghg782Nm';
webhook = 'http://test.com'; 

//store the key/value in Redis   
client.set(token, webhook, function(err, reply) {
        if (err) {
          console.log(err);
        }
});

//get the value 
client.get(token, function(err, value) {
  if (err) {
   console.log(err);
  }
console.log(value); //return 'http://test.com'

});

I get the right value here but when I want to get it from a different file (somewhere_else.js), the value is null.

somewhere_else.js

var client = redis.createClient();

token ='ghg782Nm';

//get the value 
client.get(token, function(err, value) {
  if (err) {
   console.log(err);
  }
console.log(value); //Return null

});

What did I miss? Thanks a lot!

3
  • 1
    Did you try using the same client instance between the two files? Perhaps the set() hasn't finished before you call get() from the other file? Commented Aug 8, 2015 at 1:43
  • @mscdex How did I use the same client from the other file? Commented Aug 8, 2015 at 7:39
  • Create the client in a separate file, and export it as a module. Then require this file in store.js and somewhere_else.js, and use it for both the operations. But make sure you're calling get only after set is finished. Commented Aug 9, 2015 at 11:57

2 Answers 2

1

I test your code but everything ok. Run score.js first then run somewhere_else.js. If you get null may be, get() run before set()

Sign up to request clarification or add additional context in comments.

1 Comment

It's not possible, I run store.js first, get the "ok" reply from redis and then run somewhere_else.js.
1

Have you checked if the value resides in redis using redis-cli ? If yes then do

Redis.select(0, d function(.......)

It will select the database 0 although default is zero but just to be sure if the config is not tampered with.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.