Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在Node.js中的Redis中设置键_Node.js_Redis - Fatal编程技术网

Can';t在Node.js中的Redis中设置键

Can';t在Node.js中的Redis中设置键,node.js,redis,Node.js,Redis,我正在尝试在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 */ cons

我正在尝试在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

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

下面是一个如何设置和获取“test”的示例:

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

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

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

});