Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

node.js - MongoDB: Error setting TTL index on collection : sessions

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

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

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

Example using connect-mongo's Callback

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

Simple Mongoose Example

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...