I am trying to use an AWS DocumentDB (AWS-branded MongoDB) to help me store session data. I've already got a successful connection, with mongoose, to the database in my db.js file.
When I try to pass this mongoose connection to the as the mongooseConnection parameter in my MongoStore constructor, I get the following error:
Assertion failed: You must provide either mongoUrl|clientPromise|client in options
xxxx/node_modules/connect-mongo/build/main/lib/MongoStore.js:119
throw new Error('Cannot init client. Please provide correct options');
^
Error: Cannot init client. Please provide correct options
My db.js looks like this:
import * as fs from 'fs';
import mongoose from 'mongoose';
var ca = [fs.readFileSync("./certs/rds-combined-ca-bundle.pem")]; // AWS-provided cert
var connectionOptions = {
useUnifiedTopology: true,
useNewUrlParser: true,
ssl: true,
sslValidate: true,
checkServerIdentity: false,
sslCA: ca,
replicaSet: 'rs0',
readPreference: 'secondaryPreferred',
retryWrites: false
};
var connectionString = 'mongodb://' + user + ':' + pwd + '@' + dbServerLocation + '/' + dbName; // variables defined elsewhere and removed from this post.
mongoose.connect(connectionString, connectionOptions)
.catch((err) => console.log(err));
}
export default mongoose.connection;
and my server.js (main) that is throwing the error looks like this:
import db from './db/db.js';
import session from 'express-session';
import MongoStore from 'connect-mongo';
db.on('error', () => console.error('MongoDB connection error.'));
db.on('reconnectFailed', () => console.error("Reconnection attempts to DB failed."));
db.on('connected', () => { console.log('Connected to DocumentDB database in AWS') });
import express from 'express';
const sessionStore = new MongoStore({
mongooseConnection: db,
collection: 'sessions'
})
var app = express();
app.use(session({
resave: false,
secret: 'secret word',
saveUninitialized: true,
store: sessionStore,
cookie: {
maxAge: 1000 * 60 * 60 * 24
}
}))
... and the rest of the app.
What do I need to change in order to be able to use my mongoose connection object as the session store?
I have looked elsewhere but questions like this indicate we should be able to send the actual mongoose connection rather than re-sending the connection string and doubling up on connections: Error: Cannot init client | mongo-connect express-session
Turns out the mongooseConnection parameter to the MongoStore constructor has been changed to 'client' per the documentation here: https://www.npmjs.com/package/connect-mongo
(I still get errors - specifically now I'm getting a 'con.db' is not a function from MongoStore ... but as it relates to the OP, the answer is to change to 'client' instead of 'mongooseConnection'.)
For those coming after me - specifically those that think they have a mongo connection and want to pass it as the
client
parameter.... you'll need to invoke thegetClient()
function to help make that happen - like this:Found this in the migration wiki for connect-mongo here: https://github.com/jdesboeufs/connect-mongo/blob/HEAD/MIGRATION_V4.md