MongoDB:在集合上设置TTL索引时出错:会话 [英] MongoDB: Error setting TTL index on collection : sessions

查看:95
本文介绍了MongoDB:在集合上设置TTL索引时出错:会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,该错误消息开始很少出现,但开始出现得更频繁,现在运行我的应用程序时出现4/5次.

Initially this error message started appearing very infrequently, but started to appear more regularly and now appears 4/5 times I run my application.

我正在使用Mongo处理会话存储,据我了解,TTL索引用于使会话数据过期.

I'm handling my session store with Mongo and as I understand it, the TTL index is used to make the session data expire.

/home/dan/dev/audio-wave/node_modules/connect-mongo/lib/connect-mongo.js:161
            throw new Error('Error setting TTL index on collection : ' + s
                  ^
Error: Error setting TTL index on collection : sessions
at /home/dan/dev/audio-wave/node_modules/connect-mongo/lib/connect-mongo.js:161:23
at /home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1404:28
at /home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1542:30
at /home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:159:22
at commandHandler (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:678:48)
at Db._executeQueryCommand (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1802:12)
at Cursor.nextObject (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:729:13)
at Cursor.toArray (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:158:10)
at Cursor.toArray (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/scope.js:10:20)
at /home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1541:65

以下是将其链接在一起的代码

Here's the code that ties it together

var sessionStore = new MongoStore({ db: 'audio-drop' })
  , cookieParser = express.cookieParser('waytoblue')
  , SessionSockets = require('session.socket.io')
  , sockets = new SessionSockets(io, sessionStore, cookieParser);

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.logger('dev'));
app.use(cookieParser);
app.use(express.session({
  store: sessionStore
}));

根据Mongo Shell中的db.version(),我正在运行2.4.9,并且正在使用connect-mongo的0.4.0版本.

According to db.version() from the Mongo shell, I'm running 2.4.9 and I'm using version 0.4.0 of connect-mongo.

似乎有很多人遇到了这个问题,但是似乎大多数人都解决了证书问题,我的本地mongo没有经过身份验证的保护,所以这不是问题.有什么想法吗?

There seem to be a number of people who've hit this issue, but it seems that most of them resolved to being credential issues, my local mongo is not secured with authentication, so this can't be the problem. Any ideas?

推荐答案

正如我在您的评论中所述,本质上Express在会话存储完全连接之前就已接收连接.解决方案是等待连接发生,然后再允许您的应用程序开始监听.

As I said in your comment, essentially Express is receiving connections before the session store is fully connected. The solution is to wait for the connection to occur before allowing your application to start listening.

您可以通过在创建MongoStore时使用回调或传入已经处于活动状态的连接来避免此问题.

You can avoid this problem by using a callback on MongoStore creation, or passing in an already active connection.

var sessionStore = new MongoStore({ url: 'someConnectionUrl', db: 'audio-drop' }, function(e) {

  var cookieParser = express.cookieParser('waytoblue');
  app.use(cookieParser);

  app.use(express.session({
    store: sessionStore
  }));

  app.listen();
});

简单的猫鼬示例

var mongoose = require('mongoose');

mongoose.connect('localhost', function(e) {
  // If error connecting
  if(e) throw e;

  var sessionStore = new MongoStore({ mongoose_connection: mongoose.connection }),
      cookieParser = express.cookieParser('waytoblue');

  app.use(cookieParser);

  app.use(express.session({
    store: sessionStore
  }));

  app.listen();
});

这篇关于MongoDB:在集合上设置TTL索引时出错:会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆