Error setting TTL index on collection : sessions on socket reconnect

馋奶兔 提交于 2020-01-23 13:03:35

问题


I have a node app with mongoose and socket.io. app works fine but if somehow socket gets disconnected and while trying to reconnect following error is thrown. if i run this under forever, app will restart but after some time it will throw the same error and will go on in a loop.

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

i tried other solutions found in SO but no luck. Here are some of the configs.

package.json

"dependencies": {
        "express": "3.1.1",
        "passport": "0.1.16",
        "passport-local": "0.1.6",
        "mongoose": "latest",
        "connect-mongo": "latest",
        "async": "latest",
        "express-validator": "~0.3.2",
        "express-mailer": "~0.2.0",
        "bcrypt": "~0.7.5",
        "socket.io": "latest",
        "nodemailer": "latest",
        "underscore": "latest"
    }

app.js

app=express(),
  server = http.createServer(app),
  sessionStore= new mongoStore({
            url: config.db,
            secret: '12333434',
            maxAge: new Date(Date.now() + 3600000)});


mongoose.connect(config.db,{auto_reconnect:true});

// bootstrap passport config
require('./config/passport')(passport, config);

//var app = express()
// express settings
require('./config/express')(app, config, passport,sessionStore);
// Bootstrap routes
require('./config/routes')(app, passport);

// Start the app by listening on <port>
// Start Server w/ DB Connection
var port = process.env.PORT || config.port;
var host = ('localhost');
var db = mongoose.connection;
 db.on('connecting', function() {
    console.log('connecting to MongoDB...');
  });

  db.on('error', function(error) {
    console.error('Error in MongoDb connection: ' + error);
    mongoose.disconnect();
  });
  db.on('connected', function() {
    console.log('MongoDB connected!');
  });
  db.on('reconnected', function () {
    console.log('MongoDB reconnected!');
  });
  db.on('disconnected', function() {
    console.log('MongoDB disconnected!');
    mongoose.connect(config.db,{auto_reconnect:true});
  });
// db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
  server.listen(app.get('port'), host, function() {
    console.log('Express server listening on port ' + app.get('port'));
  });

});

config.js

 production: {
        db: 'mongodb://fsdf@rer.mongohq.com:10093/erere',
        session: {
            secret: "324@234"
        },
        port: 3000,
        origin: 'http://www.123.com'
    }

来源:https://stackoverflow.com/questions/20887737/error-setting-ttl-index-on-collection-sessions-on-socket-reconnect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!