title: Mongoose 'model is not a constructor' error
body: <p>So this seems like a fairly popular error. And after seeing all the previous questions, I am posting this as they don't solve my problem. Most popular errors were using <code>module.export</code> instead of <code>module.exports</code> and calling a new instance of the schema instead of the model. I am making none of these mistakes. Here's the code.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const db = require('mongoose');
  
  db.Promise = global.Promise;
  
  db.connect(process.env.MONGODB_URI || 'mongodb://localhost/mydb').then(
      () =&gt; {console.log('Connection to database established')}
  ).catch((err) =&gt; {
      console.log(err);
  });
  
  module.exports = {db};</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const {db} = require('./dbconf');
  
  // Schema for users
  let UserSchema = new db.Schema({
      username : {
          type : String,
          required : true,
          trim : true,
          min : 1,
          unique: true
      },
      password : {
          type : String,
          required : true,
          trim : true,
          min : 8
      },
      name : {
          type : String,
          required : true,
          trim : true,
          min : 1
      },
      email : {
          type : String,
          required : true,
          trim : true,
          min : 1,
          unique : true
      }
  });
  
  const User = db.model('User', UserSchema);
  module.exports = {User};</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const {User} = require('../db/models');
  
  let user = new User({
      username : username,
      password : password,
      name : name,
      email : email
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>And here at <code>let user = new User({</code> I get <code>TypeError: User is not a constructor</code></p>