OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Can RedisJson be used to store express-session data instead of Redis

  • Thread starter Thread starter Bobthefishmonger
  • Start date Start date
B

Bobthefishmonger

Guest
I am trying to manually access the express-session data instead of using req.session.reload() and req.session.save(). I want to use Redisjson instead of default redis. I have the issue of the express-sesssion setting data using .get() instead of .json.get() (.get saves it as a string, which means I cannot access the session through client.json.get().

fFr reference: in express-session session.js the store is saved using:

Code:
defineMethod(Session.prototype, 'save', function save(fn) {
  this.req.sessionStore.set(this.id, this, fn || function(){});
  return this;
});

I set the store using this code:

Code:
const RedisStore = require("connect-redis").default;
const { createClient } = require("redis");
require("dotenv").config();

const RedisClient = createClient({
    url: `redis://localhost:${process.env.REDISPORT}`
});

RedisClient.connect();
const store = new RedisStore({ client: RedisClient });
const RedisJsonGet = RedisClient.json.get.bind(RedisClient.json);
const RedisJsonSet = RedisClient.json.set.bind(RedisClient.json);


const sessionmiddleware = session(
    {
    store: store,
    secret: crypto.randomBytes(32).toString("hex"),
    resave: false,
    saveUninitialized: true
    }
)

I can access RedisClient.json methods as shown in RedisJsonGet and Set, but if i tried to access them in the express-session they are undefined.

What can I do to be able to access them inside express-session?

<p>I am trying to manually access the express-session data instead of using <code>req.session.reload()</code> and <code>req.session.save()</code>. I want to use Redisjson instead of default redis. I have the issue of the express-sesssion setting data using <code>.get()</code> instead of <code>.json.get()</code> (.get saves it as a string, which means I cannot access the session through <code>client.json.get()</code>.</p>
<p>fFr reference: in express-session session.js the store is saved using:</p>
<pre><code>defineMethod(Session.prototype, 'save', function save(fn) {
this.req.sessionStore.set(this.id, this, fn || function(){});
return this;
});
</code></pre>
<p>I set the store using this code:</p>
<pre><code>const RedisStore = require("connect-redis").default;
const { createClient } = require("redis");
require("dotenv").config();

const RedisClient = createClient({
url: `redis://localhost:${process.env.REDISPORT}`
});

RedisClient.connect();
const store = new RedisStore({ client: RedisClient });
const RedisJsonGet = RedisClient.json.get.bind(RedisClient.json);
const RedisJsonSet = RedisClient.json.set.bind(RedisClient.json);


const sessionmiddleware = session(
{
store: store,
secret: crypto.randomBytes(32).toString("hex"),
resave: false,
saveUninitialized: true
}
)
</code></pre>
<p>I can access <code>RedisClient.json</code> methods as shown in RedisJsonGet and Set, but if i tried to access them in the express-session they are undefined.</p>
<p>What can I do to be able to access them inside express-session?</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top