简体   繁体   中英

passport mongoStore is throwing connect errors in mocha tests

It is only occassionaly but its annoying enough:

I have auto_reconnect: true which didn't seem to fix the problem.

mongoStore = require('connect-mongo')(session);

// Persist sessions with mongoStore
app.use(session({
    secret: 'ever loving sound of frogs',
    store: new mongoStore({
        url: config.mongo.uri,
        collection: 'sessions',
        auto_reconnect: true
    }, function () {
        console.log('db connection open');
    })
}));

I still get this error occassionally when I run my unit tests:

debugger listening on port 56141
Error: Error setting TTL index on collection : sessions

In my experience that error message is basically what you see when connect-mongo can't connect to the database, for a variety of reasons.

In your case, considering it sounds like it's intermittent, perhaps you might have a race condition between initialization and when it is used. Take a look at the following article below and maybe it will help to break out your connection step as shown there, and make sure it has completed before proceeding to creating the store.

..and, some other related links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM