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?
CodePudding user response:
There are some problems with your code.
First, the getDb
function returns promise. So you can't use the collection method right away. Remove the async
keyword because the current getDb
function is not an asynchronous function.
export function getDb() {
return db;
}
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 the await client.close();
code in finally block from connectToDatabase
.
Then you can add a document to the collection without a problem.
CodePudding user response:
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.