File tree Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Original file line number Diff line number Diff line change @@ -76,14 +76,19 @@ app.use(cookieParser());
7676
7777mongoose . 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
8893app . 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) ;
You can’t perform that action at this time.
0 commit comments