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