我有以下代码,我不确定如何检查 session key 是否已经存在,因为如果它已经存在,我不想创建另一个 redis session 。
请求对象在每次调用时都是新的,但我知道每个请求的 event.sender.id 都是相同的。
// If not set then create the session object
if (!req.session.key) {
console.log('Set session variable');
req.session.key = event.sender.id;
console.log('*** SESSION CREATED WITH ' + event.sender.id);
}
最佳答案
Server.js
const redis = require("redis");
const client = redis.createClient();
const hostname = '127.0.0.1';
const port = 6379;
client.on("error", function (err) {
console.log("Error " + err); });
var session_id_key = event.sender.id ; //your id
client.exists("key",session_id_key, function (err, replies) {
if(!err && replies===1){
console.log("THE SESSION ID ALREADY PRESENT ");
}else{
console.log("THE SESSION ID DOES NOT PRESENT");
} });
答案是: session ID 已经存在
关于node.js - 如何检查redis中是否已存在 session ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38661701/