From 8d7c68ea00fbe5d69417f5644aa19b6ec707b9e1 Mon Sep 17 00:00:00 2001 From: GF Date: Wed, 30 Sep 2020 18:10:43 +0900 Subject: [PATCH 1/2] Wait DB connection --- server/server.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/server/server.ts b/server/server.ts index 1201b99..4a44717 100644 --- a/server/server.ts +++ b/server/server.ts @@ -4,6 +4,11 @@ const { db } = require('./server/db'); const port = process.env.PORT || 3000; -db(); -app.listen(port); -console.info(`Listening to http://0.0.0.0:${port}`); +async function start_server() { + await db(); + app.listen(port, () => { + console.info(`Listening to http://0.0.0.0:${port}`); + }); +} + +start_server(); From 07cb9771d9f88de449300832c1023f5aea5d21ec Mon Sep 17 00:00:00 2001 From: GF Date: Wed, 30 Sep 2020 18:14:25 +0900 Subject: [PATCH 2/2] Wait DB connection --- server/server/db.ts | 59 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/server/server/db.ts b/server/server/db.ts index 1b9c305..4022fe8 100644 --- a/server/server/db.ts +++ b/server/server/db.ts @@ -6,36 +6,35 @@ const { collectionInfo } = require('./type'); async function mongoConnect() { // mongoDB 연결 함수 const secret = await config; - mongoose.connect(secret.mongoURI, - { - useNewUrlParser: true, - useUnifiedTopology: true, - useFindAndModify: false, - useCreateIndex: true, - }) - .then(async () => { - const collections = await mongoose.connection.db.listCollections().toArray(); - let check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'answerpapers'); - if (check === -1) { - await mongoose.connection.db.createCollection('answerpapers'); - } - check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'groups'); - if (check === -1) { - await mongoose.connection.db.createCollection('groups'); - } - check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'assignments'); - if (check === -1) { - await mongoose.connection.db.createCollection('assignments'); - } - check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'counters'); - if (check === -1) { - await mongoose.connection.db.createCollection('counters'); - } - console.log('DB와 연결되었습니다.'); - }) - .catch((e: Error) => { - throw (e); - }); + await mongoose.connect(secret.mongoURI, { + useNewUrlParser: true, + useUnifiedTopology: true, + useFindAndModify: false, + useCreateIndex: true, + }) + .then(async () => { + const collections = await mongoose.connection.db.listCollections().toArray(); + let check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'answerpapers'); + if (check === -1) { + await mongoose.connection.db.createCollection('answerpapers'); + } + check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'groups'); + if (check === -1) { + await mongoose.connection.db.createCollection('groups'); + } + check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'assignments'); + if (check === -1) { + await mongoose.connection.db.createCollection('assignments'); + } + check = collections.findIndex((coll: typeof collectionInfo) => coll.name === 'counters'); + if (check === -1) { + await mongoose.connection.db.createCollection('counters'); + } + console.log('DB와 연결되었습니다.'); + }) + .catch((e: Error) => { + throw (e); + }); } exports.db = async () => {