开发者

Redis Store doesn't have a get method?

开发者 https://www.devze.com 2023-04-05 07:54 出处:网络
http://senchalabs.github.com/connect/middleware-session.html mentions.... \"Every session store must implement the following methods: \"

http://senchalabs.github.com/connect/middleware-session.html mentions.... "Every session store must implement the following methods: "

  1. .get(sid,callback)
  2. .set(sid, session, callback)
  3. .destroy(sid, callback)

I'm using the following code to attempt to get the SID:

Node JavaScript, using Socket.io connection开发者_运维技巧

io.sockets.on('connection', function(socket) {
  var sid = socket.id;
  if (sid) {
    sessionStore.get(sid, function (error, session) {
      console.log("Connect Sid: " + sid);
    });
  }
});

And i'm getting the following error:

TypeError: Object function RedisStore(options) {
    options = options || {};
    Store.call(this, options);
    this.client = new redis.createClient(options.port || options.socket, options.host, options);
    if (options.pass) {
      this.client.auth(options.pass, function(err){
        if (err) throw err;
      });    
    }

    if (options.db) {
      var self = this;
      self.client.select(options.db);
      self.client.on("connect", function() {
        self.client.send_anyways = true;
        self.client.select(options.db);
        self.client.send_anyways = false;
      });
    }
  } has no method 'get'

Inclusion of redis

//Redis store for storage
var sessionStore = require('connect-redis')(express); 
...
...
app.use(express.session({secret: "keyboard cat",store: new sessionStore}));


Looks like you forgot to type new when you instantiated the store perhaps?


Taken from: https://github.com/visionmedia/connect-redis

This means express users may do the following, since express.session.Store points to the connect.session.Store function:

This seems to work:

express.session.Store(sid, function(){ console.log("Connect Sid: " + sid); });


I do it like this:

var RedisStore = require('connect-redis')(express); 
var sessionStore = new RedisStore; 
...
app.use(express.session({secret: "keyboard cat",store: sessionStore}));

This way you can reference session data using the sessionStore object from socket.io code later if needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消