Node + MongoDB - 连接池 - mongoldb-native

时间:2015-10-29 19:17:01

标签: javascript node.js mongodb connection-pooling

我正在尝试实现一个优雅的代码来解析与node-mongoldb-native的连接池。但是,我没有成功为此做一个模块。我确信这是关于吊装的事情,但我无法得到它。

Mongodb.js

'use strict';

var client = require('mongodb').MongoClient;
var connectionString = undefined;
var db = undefined;

switch(process.env.NODE_ENV) {
  case 'production':
    connectionString = process.env.MONGOLAB_URI;
    break;
  case 'test':
    connectionString = 'mongodb://localhost:27017/test';
    break;
  default:
    connectionString = 'mongodb://localhost:27017/development';
  }

client.connect(connectionString, function(err, database) {
  if (err) throw err;

  console.log('MongoDB running on ' + (process.env.NODE_ENV || 'development') + ' environment');

  database.on('error', function(err) {
    throw err;
  });

  db = database;
});

module.exports = db;  

App.js

var express = require('express');
var app = express();

var db = require('./lib/mongodb');

var professional = require('./routes/professional');

app.use('/professional', professional);

db.listCollections().toArray()
.then(function(items) { 
 console.log(items);
});

module.exports = app;     

控制台

Julios-MacBook-Air:klou juliocoelho$ ./bin/www 
/Users/juliocoelho/Projects/klou/app.js:10
db.listCollections().toArray()
  ^

TypeError: Cannot read property 'listCollections' of undefined
    at Object.<anonymous> (/Users/juliocoelho/Projects/klou/app.js:10:3)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/juliocoelho/Projects/klou/bin/www:3:11)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)

1 个答案:

答案 0 :(得分:1)

由于node.js的异步性质,您需要导出回调赋值方法以获取变量,并在异步执行时调用它。以下代码更好地解释了它:

'use strict';

var client = require('mongodb').MongoClient;
var connectionString = undefined;
var db, callback;

switch(process.env.NODE_ENV) {
    case 'production':
        connectionString = process.env.MONGOLAB_URI;
        break;
    case 'test':
        connectionString = 'mongodb://localhost:27017/test';
        break;
    default:
        connectionString = 'mongodb://localhost:27017/development';
}

client.connect(connectionString, function(err, database) {
    if (err) throw err;

    console.log('MongoDB running on ' + (process.env.NODE_ENV || 'development') + ' environment');

    database.on('error', function(err) {
        throw err;
    });
    db = database;
    if( typeof callback == 'function' ){
        callback(db);
    }
});


module.exports = function(cb){
    if(typeof db != 'undefined'){
        cb(foo); // If db is already defined, don't wait.
    } else {
        callback = cb;
    }
}

<强> App.js

var dbModule = require('./lib/mongodb');
dbModule(function(db){
    //Here code using db;
    db.listCollections()
        .toArray()
        .then(function(items) { 
            console.log(items);
        });
});