Skip to content

Commit

Permalink
convert mongo connection to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
raclim committed Oct 23, 2024
1 parent abc915b commit 9041dce
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ app.use(cookieParser());

mongoose.set('strictQuery', true);

const clientPromise = mongoose
.connect(mongoConnectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
socketTimeoutMS: 45000 // 45 seconds timeout
})
.then((m) => m.connection.getClient());
async function connectToMongoDB() {
try {
const mongooseConnection = await mongoose.connect(mongoConnectionString, {
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
socketTimeoutMS: 45000 // 45 seconds timeout
});
return mongooseConnection.connection.getClient();
} catch (err) {
throw new Error('MongoDB connection failed', err);
}
}

const clientInstancePromise = connectToMongoDB();

app.use(
session({
Expand All @@ -97,8 +102,7 @@ app.use(
secure: false
},
store: new MongoStore({
clientPromise,
autoReconnect: true
clientPromise: clientInstancePromise
})
})
);
Expand Down

0 comments on commit 9041dce

Please sign in to comment.