Closed jmcombs closed 3 years ago
@jmcombs Did you try to take a look at this example yet? https://github.com/jdesboeufs/connect-mongo/blob/master/example/mongoose.js
@mingchuno, I did and I am not trying to use the Promise method. I am trying to use the async/await method which should work with the client
option introduced in 4.3, should it not?
I separated out Mongoose from Express. This works well for me. Posting here as an example for anyone else to reuse.
dbService.js
const mongoose = require('mongoose');
const debug = require('debug')('app:core:db:service');
const chalk = require('chalk');
const mongoDbUrl = 'myMongoDbUrl'
const mongoDbOptions = {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
};
(async () => {
await mongoose
.connect(mongoDbUrl, mongoDbOptions)
.then(() => {
debug(`Connected to ${chalk.green('MongoDB')}`);
})
.catch((err) => {
debug(`${chalk.green('MongoDB')} connection ${chalk.red(`error`)}: ${err}}`);
});
})();
const db = mongoose.connection;
db.on('error', (err) => {
debug(`${chalk.green('MongoDB')} connection ${chalk.red(`error`)}: ${err}}`);
});
const dbClient = db.getClient();
module.exports = dbClient;
app.js
const MongoStore = require('connect-mongo');
const dbClient = require('dbService');
app.use(
session({
// Using MongoDB as session store
store: MongoStore.create({
client: dbClient, // Use Mongoose for DB Conection
ttl: 1800000, // Ages session out at 30 minutes
autoRemove: 'native', // Uses MongoDB's native Time to Live (TTL) to expire and remove records
crypto: {
secret: expressSessionSecret,
},
}),
}),
);
I'm submitting a ... [x] bug report [ ] feature request [ ] question about the decisions made in the repository [x] question about how to use this project
Summary I am attempting to migrate from
connect-mongo 3.2
to the latest4.4.1
and utilize the existing Mongoose connection into MongoDB. I have attempted two separate configurations with the same results (assume variablemongoDbUrl
is valid and accurate, please):Attempt 1
Attempt 2
Both result in the same error (below). I have also tried
clientPromise
instead ofclient
and the same error occurs. I want to keep my mongoose connection renamed todb
which is why I have not gone with the example that uses a promise.