title: "TypeError: Cannot read property 'create' of undefined"
body: <p>I'm building a simple site that handles users using Passport.js. I can run my server but when a user submits their signup information it spits out &quot;TypeError: Cannot read property 'create' of undefined&quot;.</p>
  <p>I know <em>what's</em> happening, I just can't understand <em>why</em>. The error gets thrown in api-routes.js, specifically in the api/signup function when it attempts to create a user via db.User.create. When I console.log(db), the first thing in the db object is</p>
  <p><code>undefined: 'C:\\Users\\LENOVO\\Desktop\\code\\Breel\\models\\user.js'</code></p>
  <p>...so the user model is being exported, it's just not defined?</p>
  <p>Any and all help you can lend me is greatly appreciated!</p>
  <p>user.js, from my models directory:</p>
  <pre><code>var bcrypt = require(&quot;bcryptjs&quot;);
  
  module.exports = function (sequelize, DataTypes) {
    var User = sequelize.define(&quot;User&quot;, {
      email: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true,
        validate: {
          isEmail: true,
        },
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    });
    User.prototype.validPassword = function (password) {
      return bcrypt.compareSync(password, this.password);
    };
    User.hook(&quot;beforeCreate&quot;, function (user) {
      user.password = bcrypt.hashSync(
        user.password,
        bcrypt.genSaltSync(10),
        null
      );
    });
    return User;
    };
  </code></pre>
  <p>index.js, from models (this and user.js are the only two files in my models directory)</p>
  <pre><code>&quot;use strict&quot;;
  
  const fs = require(&quot;fs&quot;);
  const path = require(&quot;path&quot;);
  const Sequelize = require(&quot;sequelize&quot;);
  const basename = path.basename(__filename);
  const env = process.env.NODE_ENV || &quot;development&quot;;
  const config = require(__dirname + &quot;/../config/config.json&quot;)[env];
  const db = {};
  
  let sequelize;
  if (config.use_env_variable) {
    sequelize = new Sequelize(process.env[config.use_env_variable], config);
  } else {
    sequelize = new Sequelize(
      config.database,
      config.username,
      config.password,
      config
    );
  }
  
  fs.readdirSync(__dirname)
    .filter((file) =&gt; {
      return (
        file.indexOf(&quot;.&quot;) !== 0 &amp;&amp; file !== basename &amp;&amp; file.slice(-3) === &quot;.js&quot;
      );
    })
    .forEach((file) =&gt; {
      const model = path.join(__dirname, file);
      sequelize[&quot;import&quot;];
      db[model.name] = model;
    });
  
  Object.keys(db).forEach((modelName) =&gt; {
    if (db[modelName].associate) {
      db[modelName].associate(db);
    }
  });
  
  db.sequelize = sequelize;
  db.Sequelize = Sequelize;
  
  module.exports = db;
  
  </code></pre>
  <p>api-routes.js, from my routes directory:</p>
  <pre><code>var db = require(&quot;../models&quot;);
  var passport = require(&quot;../config/passport&quot;);
  
  module.exports = function (app) {
    app.post(&quot;/api/login&quot;, passport.authenticate(&quot;local&quot;), function (req, res) {
      res.json(&quot;/members&quot;);
    });
    app.post(&quot;/api/signup&quot;, function (req, res) {
      console.log(req.body);
      console.log(db);
      db.User.create({
        email: req.body.email,
        password: req.body.password,
      })
        .then(function () {
          res.redirect(307, &quot;/api/login&quot;);
        })
        .catch(function (err) {
          console.log(err);
          res.json(err);
        });
    });
    app.get(&quot;/logout&quot;, function (req, res) {
      req.logout();
      res.redirect(&quot;/&quot;);
    });
    app.get(&quot;/api/user_data&quot;, function (req, res) {
      if (!req.user) {
        res.json({});
      } else {
        res.json({
          email: req.user.email,
          id: req.user.id,
        });
      }
    });
  };
  </code></pre>