MongoError: Topology is closed, please connect despite established database connection MongoError: Topology is closed, please connect despite established database connection express express

MongoError: Topology is closed, please connect despite established database connection


I've found the solution to the problem, but I'm not sure I understand the reasoning. The client.close() in the finally block of the validateUniqueUser function. It was closing the connection before the connection in the createPracticeProfile function was finished inserting the user.

When that line is taken out, the function works.


The issue is client variable needs to be reinstantiated again,

const client = new MongoClient(uri, { useUnifiedTopology: true}, { useNewUrlParser: true }, { connectTimeoutMS: 30000 }, { keepAlive: 1});

Try putting this in start of createPracticeProfile, validateUniqueUser and other functions


Configure your client connection like below example

var MongoClient = require('mongodb').MongoClient;var Server = require('mongodb').Server;var mongoClient = new MongoClient(new Server('localhost', 27017));mongoClient.open(function(err, mongoClient) {  var db1 = mongoClient.db("mydb");  mongoClient.close();});