Sequelize middleware code not recognized by node app

Omar Martin
Omar Martin used Ask the Experts™
on
I am new to sequelize and I created a middleware called "db.js" with a connection to a mysql table called "users". Everytime, I attempt to run the app.js, I am getting this error: "TypeError: app.use() requires a middleware function".

However, I have a middleware function. See the code below: Am I missing something? I have the module.exports object at the end.

Should I have initialize sequelize?

Here is the db.js file:
const Sequelize = require("sequelize");
const mysql = require("mysql");
const mysql2 = require("mysql2");

const db = {}
const sequelize = new Sequelize("smalldata", "root", "xxxx", {
    host: "localhost",
    dialect: "mysql",
    operatorsAliases: false,

    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 90000
    }
})

sequelize
    .authenticate()
    .then(() => {
        console.log('Connection has been established successfully.');
    })
    .catch(err => {
        console.error('Unable to connect to the database:', err);
    });

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db;

Open in new window


Here is the app.js file:


const sequelize = require("sequelize");
const db = require("./middleware/db");
const express = require("express");

//Initialize Middleware
app.use(cors());
app.use(expressLayouts);
app.use(logger);
app.use(db);

Open in new window



Here is the users.js file for the mysql table:

const Sequelize = require("sequelize");
const db = require("./db");

const User = db.sequelize.define(
    'users',
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },

        first_name: {
            type: Sequelize.STRING
        },

        last_name: {
            type: Sequelize.STRING
        },

        email: {
            type: Sequelize.STRING
        },

        password: {
            type: Sequelize.STRING
        }
    },

    {
        timestamps: true
    },
)

// Note: using `force: true` will drop the table if it already exists
User.sync({ force: true }).then(() => {
    // Now the `users` table in the database corresponds to the model 
    definition
    return User.create({
        first_name: 'John',
        last_name: 'Hancock',
        email:'johnhancock@gmail.com',
        password:'johnny'
    });
});

module.exports = User

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bridgemaker
Commented:
Capitalize error:
const Sequelize = require("sequelize");

instead of:

const sequelize = require("sequelize");

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial