I have implemented simple application that fetches data from MongoDB
const express = require('express')
const app = express()
const port = 3000
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const dbConnectionURL = 'mongodb://localhost:27017';
const dbName = 'todo';
const dbClient = new MongoClient(dbConnectionURL);
function findTodos(db, callback) {
const collection = db.collection('todos');
collection.find({}).toArray(function (err, todos) {
assert.equal(err, null);
console.log("Found the following records");
console.log(todos)
callback(todos);
});
}
app.get('/', (req, res) => {
dbClient.connect(function (err) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = dbClient.db(dbName);
findTodos(db, function(todosArr) {
var todos = { todos: todosArr }
res.send(todos)
dbClient.close()
});
});
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
It is basically copy-pasted from online tutorials. First time I make http request to 'http://localhost:3000/' it works. But second time I get
todo-backend/node_modules/mongodb/lib/utils.js:132
throw err;
^
AssertionError [ERR_ASSERTION]: 'MongoError: Topology was destroyed' == null
at todo-backend/index.js:15:16
at err (todo-backend/node_modules/mongodb/lib/utils.js:415:14)
at executeCallback (todo-backend/node_modules/mongodb/lib/utils.js:404:25)
at handleCallback (todo-backend/node_modules/mongodb/lib/utils.js:128:55)
at cursor._endSession.cursor._endSession (todo-backend/node_modules/mongodb/lib/operations/cursor_ops.js:207:38)
at ClientSession.endSession (todo-backend/node_modules/mongodb-core/lib/sessions.js:129:41)
at Cursor._endSession (todo-backend/node_modules/mongodb-core/lib/cursor.js:189:13)
at Cursor._endSession (todo-backend/node_modules/mongodb/lib/cursor.js:226:59)
at cursor._next (todo-backend/node_modules/mongodb/lib/operations/cursor_ops.js:207:20)
at initializeCursor (todo-backend/node_modules/mongodb-core/lib/cursor.js:766:16)
I have looked over the Internet for answer and found similar error but when people are doing more complicated things. What I did seems for more very basic and I can not find any room for change that might help.
If I remove dbClient.close() it works but then I see in MongoDB logs that number of connections is constantly growing. Of course I can introduce a flag and check if I already connected. But with this question I want to understand a root cause.