Can't SET/GET with NodeJS and Redis

user1078442 picture user1078442 · Dec 3, 2011 · Viewed 10.8k times · Source

I want to write a ueberDB Redis-Handler for my Etherpad.

I absolutely do not understand my problem because with PHP I can set and get key/values without any problems. It only does not work with NodeJS.

Here is a example code:

var redis = require("redis");
client = redis.createClient();
client.on("error", function (err) {
  console.log("Error " + err);
});

client.set("test", "string val", redis.print);
console.log(client.get("test"));

What do I wrong? Maybe somebody has a tip for me.

Answer

malletjo picture malletjo · Dec 3, 2011

First, you need to know if your redis client is connected. You can check with console.log(client) and you will see a varaible called "connected". You should see "true"

If you want to use redis in node you should use something like this

    client.set("test","val", function(err) {
        if (err) {
           // Something went wrong
           console.error("error");
        } else {
            client.get("test", function(err, value) {
                 if (err) {
                     console.error("error");
                 } else {
                     console.log("Worked: " + value);
                 }
            });
        }
    });

Keep in mind that all redis function are asynchronous.