gpt4 book ai didi

node.js - 无法在 Node.js 中的 Redis 中设置键

转载 作者:可可西里 更新时间:2023-11-01 11:13:49 25 4
gpt4 key购买 nike

我正在尝试在 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() 不是同步的。您需要提供回调作为最后一个参数,此时您的 key 已设置。

关于node.js - 无法在 Node.js 中的 Redis 中设置键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19593072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com