title: MongoError: Topology is closed, please connect despite established database connection
body: <p>I am writing a web application that uses asynchronous database requests as a part of the api. Currently, I have an async express route that awaits function returns from async functions. Both of these functions return booleans and both query the database. One works correctly, however the second one does not.</p>
  
  <p>Here is the MongoClient setup:</p>
  
  <pre><code>const MongoClient = require('mongodb').MongoClient;
  const uri = config.uri;                         // Contains custom url for accessing database
  const client = new MongoClient(uri, { useUnifiedTopology: true}, { useNewUrlParser: true }, { connectTimeoutMS: 30000 }, { keepAlive: 1});
  </code></pre>
  
  <p>where config is from a file imported as.</p>
  
  <pre><code>const config = require("./config.js");
  </code></pre>
  
  <p>and functions properly.</p>
  
  <p>Here is the express setup:</p>
  
  <pre><code>app.post("/signup", async function(request, response) {
    log("POST request at /signup");
  
    log("BEFORE UNIQUE USER");
    const isUniqueUser = await validateUniqueUser(request.body.email, request.body.password);
    log(isUniqueUser);
    const status = {
      status: null
    };
    if (isUniqueUser) {
      log("AFTER UNIQUE USER");
      let userCreated = await createPracticeProfile(request.body.email, request.body.password);
      log("user created: " + userCreated);
      if (userCreated) {
        status.status = "user_created";
      }
      response.json(status);
    } else {
      response.json(status);
    }
  
    console.log("********************************end");
  });
  </code></pre>
  
  <p>The console outputs:</p>
  
  <p>BEFORE UNIQUE USER</p>
  
  <p>true (which it should be)</p>
  
  <p>AFTER UNIQUE USER</p>
  
  <p>MongoError: Topology is closed.</p>
  
  <p>user created: undefined</p>
  
  <p>***...***end</p>
  
  <p>Here is the function for validating that a user is unique:</p>
  
  <pre><code>/*  VALIDATE_UNIQUE_USER
  USE: ensure user does not have existing profile
  PARAMS: email (string), password (string)
  RETURN: isUniqueUser (bool)
  */
  async function validateUniqueUser(email, password) {
    // connect to database
    const database = await client.connect().catch(err =&gt; {
      log("ERROR while connecting to database at: validateUniqueUser");
      console.log(err);
      client.close();
    });
  
    // database connection failed
    if (!database) {
      return false;
    }
  
    // connection successful =&gt; find user
    let user;
    try {
      user = await database.db("guitar-practice-suite").collection("users").findOne({email: email});
    } catch(err) {
      log("ERROR while finding user in database at: validateUniqueUser");
      console.log(err);
      client.close();
      return false;
    } finally {
      client.close();
      // user not found (unique)
      if (user === null || user === undefined) {
        return true;
      }
      return false;
    }
  }
  </code></pre>
  
  <p>Here is the function for inserting the user into the collections:</p>
  
  <pre><code>/* CREATE_PRACTICE_PROFILE
  USE: insert a practice profile into the database
  PARAMS: email (string), password (string)
  RETURN: userCreated (bool)
  */
  async function createPracticeProfile(email, password) {
    // hash password
    let hashedPassword;
    try {
      hashedPassword = await new Promise((resolve, reject) =&gt; {
        bcrypt.hash(password, null, null, function(err, hash) {
          if (err) {
            reject(err);
          }
          resolve(hash)
        });
      });
    } catch(err) {
      log("ERROR while hashing password at: createPracticeProfile");
      console.log(err);
      return false;
    }
  
    // connect to database
    const database = await client.connect().catch(err =&gt; {
      log("ERROR while connecting to database at: validateUniqueUser");
      console.log(err);
      client.close();
    });
  
    // database connection failed
    if (!database) {
      return false;
    }
  
    // database connection successful =&gt; insert user into database
    let insertUserToUsers;
    let insertUserToExercises;
    let insertUserToCustomExercises;
    try {
      insertUserToUsers = await database.db("guitar-practice-suite").collection("users").insertOne({email: email, password: hashedPassword});
      insertUserToExercises = await database.db("guitar-practice-suite").collection("exercises").insertOne({email: email});
      insertUserToCustomExercises = await database.db("guitar-practice-suite").collection("custom-exercises").insertOne({email: email, exercises: []});
    } catch(err) {
      log("ERROR while inserting user into database at: createPracticeProfile");
      console.log(err);
      client.close();
      return false;
    } finally {
      client.close();
      return insertUserToUsers &amp;&amp; insertUserToExercises &amp;&amp; insertUserToCustomExercises;
    }
  }
  </code></pre>