0

我正在尝试在 Node.js 中使用 Redis。我做错了什么......我无法理解它。

var redis = require("redis"), client = redis.createClient();

client.on("error", function (err) {
    console.log("error event - " + client.host + ":" + client.port + " - " + err);
});

/* first attempt */
console.log('first attempt');

var before = client.get("test");
console.log(before);

if(client.exists("test")) { 
    console.log("The value exists");
} else {
    console.log("The value doesn't exists");
    client.set("test","koen");
}

var after = client.get("test");
console.log(after);

/* second attempt */
console.log('--------------');
console.log('second attempt');

var before = client.get("test");
console.log(before);

if(client.exists("test")) { 
    console.log("The value exists");
} else {
    console.log("The value doesn't exists");
    client.set("test","koen");
}

var after = client.get("test");
console.log(after);

我无法设置任何键...控制台中显示以下内容:

first attempt
false
The value doesn't exists
false
--------------
second attempt
false
The value doesn't exists
false
4

2 回答 2

1

这是一个如何设置和获取“测试”的示例:

client.set("test", "koen", function (err, res) {
  console.log("set: ", res)

  client.get("test", function (err, res) {
    console.log("get: ", res);
  });

});

请记住,您的所有调用都必须是异步的。

于 2013-10-25T16:30:34.277 回答
1

client.set()不是同步的。您需要提供一个回调作为最后一个参数,此时您的密钥将被设置。

于 2013-10-25T16:07:29.910 回答