【问题标题】:Redis Store doesn't have a get method?Redis Store 没有 get 方法?
【发布时间】:2011-09-14 13:54:54
【问题描述】:

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

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

我正在使用以下代码尝试获取 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}));

【问题讨论】:

    标签: javascript node.js redis socket.io express


    【解决方案1】:

    您可能在实例化商店时忘记输入new

    【讨论】:

    • 我已经添加了我曾经包含的代码,并启动了redis存储。
    • 看 - 你在 sessionStore 变量上调用 .get,它指的是 RedisStore 类,而不是实例化的存储。
    • 试试var sessionStore = new ( require('connect-redis')(express) ); ... express.session({ store: sessionStore, secret: 'keyboard cat' })
    • 无论如何——这不是最好的写法,但我想它可以和你已经写的其他东西一起工作。
    • 我收到以下错误:TypeError: object is not a function
    【解决方案2】:

    取自: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); });
    

    【讨论】:

      【解决方案3】:

      我是这样做的:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多