Redis Store没有get方法?

发布于 2024-12-04 23:13:13 字数 1476 浏览 2 评论 0原文

http://senchalabs.github.com/connect/middleware-session.html提到.... “每个会话存储都必须实现以下方法:”

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

我正在使用以下代码来尝试获取 SID:

Node JavaScript,使用 Socket.io 连接

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

我收到以下错误:

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'

包含 redis

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

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}));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诠释孤独 2024-12-11 23:13:13

看来您在实例化商店时忘记输入 new 了?

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

烟火散人牵绊 2024-12-11 23:13:13

取自: 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:

这似乎有效:

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

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); });
思念绕指尖 2024-12-11 23:13:13

我是这样做的:

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

这样,如果需要,您可以稍后使用 socket.io 代码中的 sessionStore 对象引用会话数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文