my node.js code like this:
var data = "";
//redis client
client.get(key,function(err,value){
data += value;
});
//output
console.log(data);
BUT, it prints nothing. Why so? and how can i get the data out of the callback function?
thanks a lot.
Your passing the redis client a callback which will be called later (when the data is returned over the network), but then right after making the redis call you're trying to print the value before redis has sent you a value. You need to wait for redis to return a value.
Try this
var data = "";
//redis client
client.get(key,function(err,value){
data += value;
//output
console.log(data);
});
Your redis binding's client.get function is asynchronous.
It'll call callback function after it completes querying redis server. You should print output in callback.
var data = "";
//redis client
client.get(key,function(err,value){
data += value;
//output
console.log(data);
});