MongoDB: Error setting TTL index on collection : sessions

前端 未结 3 2123
执笔经年
执笔经年 2020-12-13 07:35

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

相关标签:
3条回答
  • 2020-12-13 08:01

    angular fullstack example

    It´s just to encapsulate all the other stuff inside the mongoose.connect callback function

    See my server/app.js

    /**
     * Main application file
     */
    
    'use strict';
    
    // Set default node environment to development
    process.env.NODE_ENV = process.env.NODE_ENV || 'development';
    
    var express = require('express');
    var mongoose = require('mongoose');
    var config = require('./config/environment');
    
    // Connect to database
    mongoose.connect(config.mongo.uri, config.mongo.options , function(e){
    
    
    // Populate DB with sample data
      if(config.seedDB) { require('./config/seed'); }
    
    // Setup server
      var app = express();
      var server = require('http').createServer(app);
      var socketio = require('socket.io')(server, {
        serveClient: (config.env === 'production') ? false : true,
        path: '/socket.io-client'
      });
      require('./config/socketio')(socketio);
      require('./config/express')(app);
      require('./routes')(app);
    
    // Start server
      server.listen(config.port, config.ip, function () {
        console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
      });
    
    // Expose app
      exports = module.exports = app;
    
    });
    

    Hope it helps!!

    0 讨论(0)
  • 2020-12-13 08:05

    Upgrade to connect-mongo version 0.8.0 which worked for me.

    0 讨论(0)
  • 2020-12-13 08:08

    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();
    });
    
    0 讨论(0)
提交回复
热议问题