Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

457
Views
Use database object after it has been created

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!