Javascript 在Node.js中共享mongodb连接时出现问题

Javascript 在Node.js中共享mongodb连接时出现问题,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我正在尝试编写一个服务来管理mongodb连接。我希望连接打开一次,然后重新使用: const MongoClient = require('mongodb').MongoClient; // Port it always 27017 const url = 'mongodb://localhost:27017'; // The database name const dbName = 'mongo-crud'; let db = null; // Gets an instance of

我正在尝试编写一个服务来管理mongodb连接。我希望连接打开一次,然后重新使用:

const MongoClient = require('mongodb').MongoClient;

// Port it always 27017
const url = 'mongodb://localhost:27017';

// The database name
const dbName = 'mongo-crud';

let db = null;

// Gets an instance of the db
module.exports.getDB = () => new Promise((resolve, reject) => {
  if (db) {
    console.log('already got the db');
    resolve(db);
    return;
  }

  MongoClient.connect(url, { useUnifiedTopology: true })
  .then(client => {
    console.log('make a new client');
    db = client.db(dbName);
    resolve(db);
  })
  .catch(error => {
    reject(error);
    process.exit(1);
  })
});
我在我的
App.js
中对此进行了测试:

const client = require('./MongoDBService');


client.getDB()
 .then(db => console.log('database connected'))
 .catch(error => console.log(error));

client.getDB()
  .then(db => db.collection('dogs'))
  .then(collection => collection.find().toArray())
  .then(array => console.log(array))
  .catch(error => console.error(error));

client.getDB()
  .then(db => db.collection('cats'))
  .then(collection => collection.find().toArray())
  .then(array => console.log(array))
  .catch(error => console.error(error));

当我检查控制台日志时,每次“创建新客户机”时,它似乎都在创建一个新实例,为什么我会在这里丢失
db
对象?

尝试在异步函数中运行它并等待它

const client=require('./MongoDBService');
异步函数init(){
等待client.getDB()
.then(db=>console.log('database connected'))
.catch(错误=>console.log(错误));
等待client.getDB()
.then(db=>db.collection('dogs'))
.then(collection=>collection.find().toArray())
.then(数组=>console.log(数组))
.catch(error=>console.error(error));
等待client.getDB()
.then(db=>db.collection('cats'))
.then(collection=>collection.find().toArray())
.then(数组=>console.log(数组))
.catch(error=>console.error(error));
}
init()
但是你也可以把它们都放在一个承诺链中

const client=require('./MongoDBService');
client.getDB()
.then(db=>console.log('database connected'))
.catch(错误=>console.log(错误))
.then(client.getDB())
.then(db=>db.collection('dogs'))
.then(collection=>collection.find().toArray())
.then(数组=>console.log(数组))
.catch(error=>console.error(error));
.then(client.getDB())
.then(db=>db.collection('cats'))
.then(collection=>collection.find().toArray())
.then(数组=>console.log(数组))
.catch(error=>console.error(error));
当您玩完游戏后,一定要使用async/await

const client=require('./MongoDBService');
异步函数dbStuff(){
const db=await client.getDB()
console.log('数据库已连接')
const dogsCollection=db.collection('dogs'))
常量数组=wait dogsCollection.find().toArray()
console.log(数组)
const catscolection=db.collection('cats'))
const catsArray=wait catscolection.find().toArray()
console.log(catsArray)
}
const init=async()=>{
试一试{
等待多斯塔夫
}捕获(错误){
控制台错误(错误)
}
}
init()
//或
//doStuff().catch(error=>console.error(error));

这是因为在执行另一个
getDB()
调用时,第一个调用仍然连接到数据库。啊,我看到了。我加了一个延迟,现在开始工作了。