Anna Klein
Anna Klein

Reputation: 2171

No Sequelize instance passed

I am trying to outsource my models for the dbInit function from my dbController because I have several models which makes the dbController to big.

So I am calling initDb from my db_controller.js which looks like this (I use that docu http://docs.sequelizejs.com/manual/getting-started.html )

const userModel = require('../model/user')
const subjectModel = require('../model/subject')
const Sequelize = require('sequelize')
const seq = new Sequelize({
  dialect: 'sqlite',
  storage: './user.db'
})

async function initDb () {
  await userModel.user.initUser()
  await subjectModel.subject.initSubject()
  userModel.user.userClass.hasMany(subjectModel.subject.subjectClass)
}

The user in the user.js looks like this:

const Sequelize = require('sequelize')
const seq = new Sequelize({
  dialect: 'sqlite',
  storage: './user.db'
})

class User extends Sequelize.Model {
}

exports.user = {
  initUser: initUser,
  userClass: User
}

async function initUser () {
  return new Promise(resolve => {
    User.init(
      // attributes
      {
        firstName: {
          type: Sequelize.STRING,
          allowNull: false
        },
        lastName: {
          type: Sequelize.STRING,
          allowNull: false
        },
        email: {
          type: Sequelize.STRING,
          allowNull: false
        }
      },
      // options
      {
        seq,
        modelName: 'user'
      }
    )
    resolve()
  })
}

and pretty much the same for the subject.js

const Sequelize = require('sequelize')
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: './user.db'
})

class Subject extends Sequelize.Model {
}

exports.subject = {
  initSubject: initSubject,
  subjectClass: Subject
}

async function initSubject () {
  return new Promise(resolve => {
    Subject.init(
      // attributes
      {
        name: {
          type: Sequelize.STRING,
          allowNull: false
        }
      },
      // options
      {
        seq: sequelize,
        modelName: 'subject'
      }
    )
    resolve()
  })
}

So when I try to execute this via node db_controller.js

I receive this error (shortened)

(node:12444) UnhandledPromiseRejectionWarning: Error: No Sequelize instance passed
    at Function.init (D:\Git\ppb\node_modules\sequelize\lib\model.js:915:13)
    at resolve (D:\Git\ppb\src\model\user.js:26:10)
    at new Promise (<anonymous>)
    at Object.initUser (D:\Git\ppb\src\model\user.js:25:10)
    at initDb (D:\Git\ppb\src\controller\db_controller.js:18:24)
    at Object.<anonymous> (D:\Git\ppb\src\controller\db_controller.js:45:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)

Thank you very much for any advice in advance!

Upvotes: 10

Views: 22954

Answers (2)

user15626187
user15626187

Reputation: 11

import database from 'path/some*.ts';

this is my init database file when used in express app.ts; also in this database file I export the sequelize instance;

import User from '../some/model/define/file';

after database to import Sequelize can auto sync define to schema ,the models like this:

Class User extends Model {...} 
User.define(...);
User.has or many();

this can solve the problem: sequelize instance error.

point: behind your sequelize reference order!!!!

Upvotes: 1

Catalyst
Catalyst

Reputation: 3237

I thought it was peculiar that you are passing seq instead of sequelize in the options.

Checking the docs here http://docs.sequelizejs.com/ I see in the example that they pass the property sequelize and not seq.

So I recommend changing:

{
  seq: sequelize,
  modelName: 'subject'
}

to

{
  sequelize: sequelize,
  modelName: 'subject'
}

Upvotes: 14

Related Questions