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?
There are some problems with your code.
First, the getDb function returns a promise. Therefore, you cannot use the collection method right away. Remove the async keyword because the current getDb function is not an async function.
export function async() { return db; } Second, in the test statement and finally, the block is finally executed unconditionally, regardless of whether an exception is thrown in the code. Therefore, after displaying the connection success log in connectToDatabase , the connection is immediately closed. To insert the data, the connection must be connected, so clear the await client.close(); block code finally from connectToDatabase .
You can then add a document to the collection without any 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):
class Collection {
constructor(db) {
this.db = db
//do whatever else needs to be setup
}
use(name) {
//or use it here
this.db.use(name)
}
}
export default Collection
Then in your main file some code:
import Collection from './Collection.js'
const collection = new Collection(getDb)
collection.use('some arbitrary name')
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.