title: MongoClient not connected error while trying to use Mongoose?
body: <p>As part of my course I'm learning mongodb and now Mongoose. I've written the code exactly as done in the lesson but when trying to start it with node app.js I get the following error:</p>
  <p><code>$ node app.js  (node:25772) </code> <strong>UnhandledPromiseRejectionWarning: MongoNotConnectedError: MongoClient must be connected to perform this operation</strong>
  <code>at Object.getTopology (C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\node_modules\mongodb\lib\utils.js:391:11) at Collection.insertOne (C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\node_modules\mongodb\lib\collection.js:150:61) at NativeCollection.&lt;computed&gt; [as insertOne] (C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:200:33) at NativeCollection.Collection.doQueue (C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\lib\collection.js:135:23) at C:\Users\donal\OneDrive\Desktop\Udemy Web Development\FruitsProject\node_modules\mongoose\lib\collection.js:82:24 at processTicksAndRejections (internal/process/task_queues.js:77:11) (Use </code>node --trace-warnings ...<code>to show where the warning was created) (node:25772) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag</code>--unhandled-rejections=strict<code> (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:25772) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.</code></p>
  <p>I have mongosh running in a separate tab in the terminal and I've tried several searches to look for similar problems. Any help very welcome!</p>
  <p>Here's my app.js code in full:</p>
  <pre><code>
  
  mongoose.connect(&quot;mongodb://localhost:27017/fruitsDB&quot;);
  
  
  const fruitSchema = new mongoose.Schema({
    name: String,
    rating: Number,
    review: String
  });
  
  const Fruit = mongoose.model(&quot;Fruit&quot;, fruitSchema);
  
  const fruit = new Fruit({
    name: &quot;Apple&quot;,
    rating: 7,
    review: &quot;Pretty solid as a fruit&quot;
  });
  
  fruit.save();
  mongoose.connection.close();
  
  
  const findDocuments = function(db, callback) {
  
    const collection = db.collection('fruits');
  
    collection.find({}).toArray(function(err, fruits) {
      assert.equal(err, null);
      console.log(&quot;Found the following records&quot;);
      console.log(fruits)
      callback(fruits);
  
    });
  }
  </code></pre>