Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:56:47+00:00 2026-06-18T05:56:47+00:00

I wrote what is possibly the smallest wrapper around node-mongodb-native wrapper. But, I feel

  • 0

I wrote what is possibly the smallest wrapper around node-mongodb-native wrapper. But, I feel that it needs improving.
It’s so small it fits here comfortably:

function MongoWrapper() {
  this.db = null;
};

var mongoWrapper;
module.exports = exports = mongoWrapper = new MongoWrapper;

// This means that you can do `new include('mongoWrapper').MongoWrapper()`
mongoWrapper.MongoWrapper = MongoWrapper;

// ObjectId is the most handy method of all. This will work with
// native BSON or Pure BSON
mongoWrapper.ObjectId = function() {
    if (!mongo.BSONNative || !mongo.BSONNative.ObjectID) {
      return function(id) {
            return mongo.BSONPure.ObjectID.createFromHexString(id);
      };
    }
    return function(id) {
            return new mongo.BSONNative.ObjectID(id);
    };
}();

MongoWrapper.prototype.connect = function(url, options, cb ){

  var that = this;
  var MongoClient = mongo.MongoClient;

  MongoClient.connect( url, function( err, db ){

    if( err ) {
      console.log( err );
    } else {
      that.db = db; 
    }
    cb( err, db );
  });
}

Now… The “problem” with this is that I need to wrap my whole server in a callback:

mw.connect('mongodb://localhost/hotplate', {}, function( err, db ){
    app.configure(function(){
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    ...
    app.use(express.session({
      // secret: settings.cookie_secret,
      secret: 'woodchucks are nasty animals',
      store: new MongoStore({
        // db: settings.db
        // db: hotplate.get('db').client
         db: db
      })
    }));

Other drivers (like Mongoose, or even mongojs) manage not to force to use the callback. I looked at their code, and… well, I didn’t quite get it. Mongojs in particular seems to use a library for promises, but I am having trouble understanding it.
Note that express.session for example wants, as a parameter, a fully working connection (which is what I do here). Without using the connection, you cannot actually be sure that the connection will be set.

So: what’s the easiest way to get rid of the need for a callback?

The basic idea, I suppose, would be to “clone” the mongodb API calls, wrapping them with code to handle the possibility that the “db” variable is not set. But… how would that work?

Any help would be greatly appreciated!

Merc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:56:49+00:00Added an answer on June 18, 2026 at 5:56 am

    Eventually, you’ll hit the situation where you absolutely need to wait for the connection to complete before continuing as it is async. And without a callback, it won’t work (as the MongoClient requires a callback).

    You could use an Event to wrap it — but that’s just a different type of callback really (conceptually). That’s what Mongoose does — it raises an event when the connection is ready, open.

    Using Node.js, there isn’t a solution that doesn’t involve either an event or callback, somewhere (that’s an intentional design choice of Node and the MongoDB driver). It’s an async connection in the driver. You just need to delay some of the express setup until after the connection is opened. It only needs to happen at app startup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small PHP application that I'd like to distribute. I'm looking for
So I wrote this function that is given possible numbers, and it has to
Its possibly im just missing something here but, when I write some code for
I wrote a website a while ago that is a little messy in how
Several years ago, I wrote a small Cocoa/Obj-C game framework for OpenGL ES 1.1
I wrote following code: class node: def __init__(self, title, series, parent): self.series = series
I wrote an android application that captures a live preview from the camera. It
I've recently started learning Python and wrote a little script that informs me when
For recreational reasons I wrote a PHP class that classifies files with tags instead
I'm P/Invoking out to Graphviz as shown here . When I wrote that blog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.