在集合上设置TTL索引时出错:套接字重新连接时的会话

时间:2014-01-02 16:57:39

标签: node.js mongodb socket.io mongoose connect

我有一个带有mongoose和socket.io的节点应用程序。应用程序工作正常,但如果以某种方式套接字断开连接,并尝试重新连接跟随错误被抛出。如果我永远运行它,应用程序将重新启动,但一段时间后它将抛出相同的错误,并将继续循环。

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

我尝试了在SO中找到的其他解决方案,但没有运气。以下是一些配置。

的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'
    }

0 个答案:

没有答案