Nodejs mongoclient集合导出

时间:2015-12-09 21:38:11

标签: mongodb

这是connection.js

id

这是product.js

var MongoClient  = require('mongodb').MongoClient;
var db_singleton = null;

var getConnection= function getConnection(callback) {
    if (db_singleton) {
        callback(null,db_singleton);
    } else {
        var connURL = "mongodb://localhost:27017/testdb";
        MongoClient.connect(connURL,function(err,db){
            if(err)
                console.log("Error creating new connection "+err);
            else
            {
                db_singleton=db;
                console.log("created new connection");
            }
            callback(err,db_singleton);
        });
    }
};

module.exports = getConnection;

console.log(产品)始终为null。 但它应该是object.I想在服务器启动时缓存变量。

0 个答案:

没有答案
相关问题