I'm building a project that uses SQL for the database, node,express and sequelize for the backend.
I'm trying to use sequelize for my project and am running into an "TypeError: sequelize.define is not a function".
I'm not exactly sure what the problem is, I've looked at other posts and made various changes with no luck.
The first error starts at the define.
Error:
userModel.js:4
const User = sequelize.define('user', {
My dbConfig.js file
module.exports = {
HOST: 'localhost',
USER: 'placeholder',
PASSWORD: 'placeholder',
DB: 'placeholder',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
}
My index.js inside my models folder
const dbConfig = require('../config/dbConfig.js')
const { Sequelize, DataTypes } = require('sequelize')
const sequelize = new Sequelize(
dbConfig.DB,
dbConfig.USER,
dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
operatorsAliases: false,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle
}
}
)
sequelize.authenticate()
.then(() => {
console.log('Connected...')
})
.catch(err => {
console.log(`Error ${err}`)
})
const db = {}
db.Sequelize = Sequelize
db.sequelize = sequelize
// ----- Put tables models here -----
// This maps the tables to object
db.users = require('./userModel.js')(Sequelize, DataTypes)
// ----- IMPORTANT -----
// If force = true, everytime the server runs we will lose the data for the table
// force = true recreates a table
db.sequelize.sync({ force: false })
.then(() => {
console.log('Re-sync done')
})
module.exports = db
My userModel.js file
// DataTypes Documentation - https://sequelize.org/master/variable/index.html#static-variable-DataTypes
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('user', {
userID: {
type: DataTypes.INTEGER,
allowNull: false
},
userName: {
type: DataTypes.STRING
},
password: {
type: DataTypes.STRING
},
firstName: {
type: DataTypes.STRING
},
lastName: {
type: DataTypes.STRING
},
phoneNumber: {
type: DataTypes.STRING
},
emergencyContact: {
type: DataTypes.STRING
},
verified: {
type: DataTypes.BOOLEAN
}
})
return User
}
My app.js file where the server starts
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan') // shows which device or browser access
const PORT = process.env.PORT || 8081
const app = express() // builds express server for API.
// Middle Ware
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(morgan('combined'))
app.use(bodyParser.json()) // allows app to easily parse JSON request.
app.use(cors()) // to allow any host or client to access the server.
// ----- Routers -----
const router = require('../routes/userRouter.js')
app.use('/api/users', router)
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`)
})
CodePudding user response:
You should call define
on Sequelize instance and not on Sequelize itself:
db.users = require('./userModel.js')(sequelize, DataTypes)
Also please look at another my answer to better understand how to register models and their associations