node-mongodb-native: How can I share the db api object of the connection callback through m…

微信扫一扫,分享到朋友圈

node-mongodb-native: How can I share the db api object of the connection callback through m…

I am currently rolling back from mongoose to node-mongodb-native.

So I am quite new at this topic. However my issue currently is that want to create a database collection on server start which I then can use through the application. Unfortunately I only find examples in the repository where you only can do database actions directly in the callback of the connect function.

docs:

var mongodb = require("mongodb"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer);

db_connector.open(function(err, db) {
    if (err) throw new Error(err);

    // here I can do my queries etc.
});

But how can I get access to the db object in the callback when I am in some route callback?
Currently the only idea I would have is wrapping the application into the callback:

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer);

var app = new express();

db_connector.open(function(err, db) {
    if (err) throw new Error(err);

    app.get('/products', function(req, res, next) {
        db.collection('products', function(err, collection) {
            if (err) next(new Error(err));
            collection.find({}, function(err, products) {
                res.send(products);
            });
        });
    });

});

But I do not think this is the way it should meant to be?

Isn’t there the way to create a sync database connection call which I then can easily use through the whole application how it was by mongoose?

Regards bodo

Problem courtesy of: bodokaiser

Solution

Db.open opens the connection to mongodb and returns a reference to itself. See here for the sourcecode: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/db.js#L245

All you want is to hold off on starting your express app listening on it’s port and receiving requests until your connection to mongodb has been established.

So what you could do is this:

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer),
    db;

var app = new express();

app.get('/products', function(req, res, next) {
  db.collection('products', function(err, collection) {
    if (err) next(new Error(err));
    collection.find({}, function(err, products) {
      res.send(products);
    });
  });
});

db_connector.open(function(err, opendb) {
  if (err) throw new Error(err);
  db = opendb;
  app.listen(3000);
});

What I’m not sure about though is whether this is a good idea. This solution doesn’t allow you to recreate your connection if there has been a connection break or you restarted your mongodb process. So while the above might work, it might be a better idea to create a method that will wrap the creation of a connection to mongodb.

Solution courtesy of: AndyD

微信扫一扫,分享到朋友圈

node-mongodb-native: How can I share the db api object of the connection callback through m…

Telegram desktop app leaked internet addresses when starting calls

上一篇

GIVEAWAY: Killer-powered Linksys WRT32X routers and MSI Z370

下一篇

你也可能喜欢

node-mongodb-native: How can I share the db api object of the connection callback through m…

长按储存图像,分享给朋友