From 9041dce27efd95f396b479fbc328991fb3e6fc44 Mon Sep 17 00:00:00 2001 From: raclim Date: Wed, 23 Oct 2024 12:51:03 -0400 Subject: [PATCH] convert mongo connection to async/await --- server/server.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/server/server.js b/server/server.js index 8b4d19645..afb9530cf 100644 --- a/server/server.js +++ b/server/server.js @@ -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({ @@ -97,8 +102,7 @@ app.use( secure: false }, store: new MongoStore({ - clientPromise, - autoReconnect: true + clientPromise: clientInstancePromise }) }) );