Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on sequelize.import: defineCall is not a function

The error I'm getting is this:

C:\Users\brend\project\server\node_modules\sequelize\lib\sequelize.js:392
      this.importCache[path] = defineCall(this, DataTypes);
                               ^
TypeError: defineCall is not a function
    at Sequelize.import (C:\Users\brend\project\server\node_modules\sequelize\lib\sequelize.js:392:32)
    at fs.readdirSync.filter.forEach (C:\Users\brend\project\server\src\models\index.js:20:35)

I'm learning as I go with node, vue and a bunch of libraries, and I haven't been able to figure this out.

index.js is as following:

const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const config = require('../config/config')
const db = {}

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)
    db[model.name] = model
  })

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

And here is my User.js, which is the only model I have currently:

module.exports = (sequelize, DataTypes) =>
  sequelize.define('User', {
    email: {
      type: DataTypes.STRING,
      unique: true
    },
    password: DataTypes.STRING
  })

And if necessary, my folder structure:

-src
--app.js
--routes.js

--config
----config.js

--controllers
----AuthenticationController.js

--models
----index.js
----User.js

There's also the thing that in my app.js sequelize.sync() is not defined, at least that's what my IDE indicates.

like image 850
Brendan Avatar asked Nov 02 '17 16:11

Brendan


1 Answers

I found this on github sequelize github, replace

const model = sequelize.import(path.join(__dirname, file));

with

var model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);

It works for me with sequelize 6.3.0

like image 145
artfulbeest Avatar answered Sep 30 '22 16:09

artfulbeest