I'm new to express.
I want to import files to sequelize and declared:
const model = sequelize.import(path.join(__dirname, file)) ^
It returned the following type error
TypeError: sequelize.import is not a function
And then, edited code to
var model = require(path.join(__dirname, file))(sequelize, Sequelize); ^
Then the error is:
TypeError: require(...) is not a function
I think it is the error in importing stuff....
Here is my whole file code:
const fs = require('fs'); const path = require('path'); const Sequelize = require('sequelize'); const config = require('../config/config'); const db = {} var __dirname = path.resolve(); const sequelize = new Sequelize( config.db.database, config.db.user, config.db.password, config.db.options ) fs .readdirSync(__dirname) .filter((file) => file !== 'index.js' ) .forEach((file) => { //const model = sequelize.import(path.join(__dirname, file)) var model = require(path.join(__dirname, file))(sequelize, Sequelize); db[model.name] = model }) db.sequelize = sequelize db.Sequelize = Sequelize module.exports = db
To import Sequelize models, you need to export the model as a module first. The code above creates a new instance of the Sequelize class to connect to your database. The sequelize instance needs to be passed into the user. js model file when you call the require() function.
Models can be defined in two equivalent ways in Sequelize: Calling sequelize.define(modelName, attributes, options) Extending Model and calling init(attributes, options)
The Sequelize instance method sync() is used to synchronize your Sequelize model with your database tables. The synchronization happens at the table level. When your table doesn't exist the sync() method will generate and run a CREATE TABLE statement for you.
The error is caused by using the sequelize import object. Instead you should use Node's built in CommonJS require
function. So change this line in your models/index.js
:
const model = sequelize['import'](path.join(__dirname, file))
to:
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes)
You can also just regenerate the models
directory and readd your models without the old index.js
if you find that easier:
mv models models.bak && sequelize init:models && mv models.bak/index.js models.bak/index.js.bak && mv models.bak/* models/ && rm models.bak
That one liner will fix your problem if you have sequelize-cli installed globally. If you don't you can use this one:
npm i --save-dev sequelize-cli && mv models models.bak && npx sequelize init:models && mv models.bak/index.js models.bak/index.js.bak && mv models.bak/* models/ && rm models.bak
You may also need to update your config folder. I use a JavaScript config to inject ENVs, so I had to add to change my const config = require(...
line to reflect that. If you used one of my one liners your old models/index.js
file is now at index.js.bak if you need to grab any custom stuff from it.
As of now I was able to fix the issue by downgrading the sequelize module version in your package.json to "sequelize": "^5.22.3",
. do let me know if it is also fixed on your side.
Edit: any sequelize version under < 6.0.0
should work as normal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With