Учитывая следующий код:
db.js
// Connecting to catalogstore (mongodb)
const mydb = async () => {
try {
await mongoose.connect(process.env.db);
console.log("Connected to Database!");
}
catch (err) {
throw new Error("Database connection error:", err);
}
};
export { db }
app.js
import { db } from './db';
db().then(async() => {
try {
let server = app.listen(process.env.port,
process.env.host, function() {
let host = server.address().address;
let port = server.address().port;
console.log('App started');
});
} catch (err) {
console.log(err);
}
});
Как правило, я хотел бы запустить сервер Express только после установления соединения с БД.
На самом деле работает нормально, однако я получаю это предупреждение:
(node:29892) UnhandledPromiseRejectionWarning: Error: Database connection error:
at catalogstore (/Users/notaris/Workspace/Google/gcp-devops/apps/catalogservice/src/db.js:44:11)
at processTicksAndRejections (internal/process/task_queues.js:89:5)
(node:29892) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:29892) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Как мне правильно с этим справиться?