I’m trying to build an express server.
In the app.js
file I create the server and try to connect to my mongodb.
import express from "express";
import dbo from "./db/conn.js";
const app = express();
app.listen(PORT, async () => {
await dbo.connectToDatabase();
log.info(`Server is running on port: ${PORT}`);
});
In file conn.js
const client = new MongoClient(uri);
let db: Db;
export async function connectToDatabase() {
try {
await client.connect();
db = client.db(dbName);
await db.command({ ping: 1 });
log.info(`Successfully connected to database: ${db.databaseName}.`);
} finally {
await client.close();
}
}
export async function getDb() {
return db;
}
export default { connectToDatabase, getDb };
And now in a seperate file, I wantt to use the database connection I created in the app.js
file.
like this:
const collection = getDb().collection(collectionName);
// insert documents in collection.
but when I try to run the app I get error TypeError: Cannot read properties of undefined
.
So, is there a way to wait until the db connection is established?
2
Answers
There are some problems with your code.
First, the
getDb
function returns promise. So you can’t use the collection method right away. Remove theasync
keyword because the currentgetDb
function is not an asynchronous function.Second, in the try & finally statement, the finally block is executed unconditionally regardless of whether an exception occurs in the code. Therefore, after displaying the connection success log in the
connectToDatabase
, the connection closes immediately. In order to insert the data, the connection must be connected, so please erase theawait client.close();
code in finally block fromconnectToDatabase
.Then you can add a document to the collection without a problem.
One way to accomplish what you’re trying to do is to pass that connection to a class or function you define elsewhere.
Say this is your collection code (or whatever thing even outside a collection that needs a raw db connection instance in order to use):
Then in your main file some code:
This is just one way to do it but there are other ways as well. Also if this is the only thing using your getDb() method above, you can remove it from your main and put it in here as a manager.