Skip to content

Commit 9041dce

Browse files
committed
convert mongo connection to async/await
1 parent abc915b commit 9041dce

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Diff for: server/server.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,19 @@ app.use(cookieParser());
7676

7777
mongoose.set('strictQuery', true);
7878

79-
const clientPromise = mongoose
80-
.connect(mongoConnectionString, {
81-
useNewUrlParser: true,
82-
useUnifiedTopology: true,
83-
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
84-
socketTimeoutMS: 45000 // 45 seconds timeout
85-
})
86-
.then((m) => m.connection.getClient());
79+
async function connectToMongoDB() {
80+
try {
81+
const mongooseConnection = await mongoose.connect(mongoConnectionString, {
82+
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
83+
socketTimeoutMS: 45000 // 45 seconds timeout
84+
});
85+
return mongooseConnection.connection.getClient();
86+
} catch (err) {
87+
throw new Error('MongoDB connection failed', err);
88+
}
89+
}
90+
91+
const clientInstancePromise = connectToMongoDB();
8792

8893
app.use(
8994
session({
@@ -97,8 +102,7 @@ app.use(
97102
secure: false
98103
},
99104
store: new MongoStore({
100-
clientPromise,
101-
autoReconnect: true
105+
clientPromise: clientInstancePromise
102106
})
103107
})
104108
);

0 commit comments

Comments
 (0)