I am trying to write a service to manage the mongodb connection. I want the connection to open once and then be reused:
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);
})
});
I test this in my 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));
When I check the console log it appears to be creating a new instance each time "make a new client"
, why am I losing the db
object here?
Try running it inside an async function and wait for it
const client = require('./MongoDBService');
async function init () {
await client.getDB()
.then(db => console.log('database connected'))
.catch(error => console.log(error));
await client.getDB()
.then(db => db.collection('dogs'))
.then(collection => collection.find().toArray())
.then(array => console.log(array))
.catch(error => console.error(error));
await client.getDB()
.then(db => db.collection('cats'))
.then(collection => collection.find().toArray())
.then(array => console.log(array))
.catch(error => console.error(error));
}
init()
But you could also but them all together in a promise chain
const client = require('./MongoDBService');
client.getDB()
.then(db => console.log('database connected'))
.catch(error => console.log(error))
.then(client.getDB())
.then(db => db.collection('dogs'))
.then(collection => collection.find().toArray())
.then(array => console.log(array))
.catch(error => console.error(error));
.then(client.getDB())
.then(db => db.collection('cats'))
.then(collection => collection.find().toArray())
.then(array => console.log(array))
.catch(error => console.error(error));
And when you are done playing you should definitely use async/await
const client = require('./MongoDBService');
async function dbStuff () {
const db = await client.getDB()
console.log('database connected')
const dogsCollection = db.collection('dogs')
const array = await dogsCollection.find().toArray()
console.log(array)
const catsCollection = db.collection('cats'))
const catsArray = await catsCollection.find().toArray()
console.log(catsArray)
}
const init = async () => {
try {
await doStuff()
} catch(error) {
console.error(error)
}
}
init()
// OR
// doStuff().catch(error => console.error(error));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.