title: Connecting to open sequelize connection
body: <p>I created an Ionic app and have the front-end working. Now I'm focusing on the backend using Node and Sequelize for my PostgreSQL db connection.</p>
  <p>I created the connection in <code>bin/www</code> and am trying to use it in my <code>models/user.js</code> file (to create a user model), and in one of my route files (<code>index.js</code>) to try to create a new user and add it to the db.</p>
  <p>I haven't used Sequelize before, so I'm not sure if I have the organizational flow right or not. I'm trying to use <code>module.exports</code> to pass the connection from one file to another, but it's actually passing <code>undefined</code>.</p>
  <pre><code>&quot;GET /api/home 500 7.717 ms - 2567 TypeError: Cannot read property 'sync' of undefined
          at /Users/me/app/server/routes/api/index.js:13:12&quot;
  </code></pre>
  <p>Is there a better way to do this?</p>
  <p>This is the <code>bin/www</code> file:</p>
  <pre><code>var pg = require('pg');
  var Sequelize = require(&quot;sequelize&quot;);
  
  //DB CONNECTION
  var sequelize = new Sequelize('dbname', 'postgres', 'password', {
    host: 'localhost',
    port: '5434',
    dialect: 'postgres',
    pool: {
      max: 5,
      min: 0,
      idle: 10000
    },
  });
  
  sequelize.authenticate().then(function(err) {
      if (err) console.log('Unable to connect to the PostgreSQL database:', err);
      console.log('Connection has been established successfully.');
  });
  
  module.exports.sequelize = sequelize;
  </code></pre>
  <p>This is my <code>models/user.js</code>:</p>
  <pre><code>var Sequelize = require(&quot;sequelize&quot;);
  var sequelize = require(&quot;../bin/www&quot;).sequelize;
  
  module.exports = function(sequelize, DataTypes) {
    var User = sequelize.define('user', {
      fb_access_token: {
        type: Sequelize.STRING
      },
      fb_id: {
        type: Sequelize.STRING
      },
      book_wishlist: {
        type: Sequelize.ARRAY
      }
    }, {
      freezeTableName: true
    });
    return User;
  }
  </code></pre>
  <p>And this is the <code>index.js</code> route:</p>
  <pre><code>var Sequelize = require(&quot;sequelize&quot;);
  var express = require('express');
  var router = express.Router();
  var sequelize = require(&quot;../../../bin/www&quot;).sequelize;
  var User = require('../../../models/user');
  
  router.get('/home', function(req, res, next) {
    console.log('in router.get to home');
    console.log('User:',User);
  
    sequelize.sync({ force: true }).success(function(err) {
      //insert new user
      User.create({
        fb_access_token: &quot;yes&quot;,
        fb_id: &quot;no&quot;,
        book_wishlist: [&quot;yes&quot;, &quot;no&quot;]
      }).success(function(user) {
        //you can now access the newly created user via the variable user
        console.log(user);
        res.send(user);
      });
    });
  });
  </code></pre>