Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 669
Anna Klein Avatar asked Apr 20 '19 16:04

Anna Klein


People also ask

What is a Sequelize instance?

Instances are the individual results from a Sequelize call. In SequelizeMock, these objects have most of the same attributes as in the normal Sequelize Instance, however they also have a few additional bits of functionality.

How do I insert a Sequelize?

To insert new rows into your SQL table, you need to first create a Sequelize Model object. In Sequelize, a Model is an object that represents your SQL table, giving information to Sequelize about the name, the columns, and their data types.

How do you create a Sequelized model?

Sequelize set up Install Sequelize database driver for the database you would like to use by running one of the commands below. Install npm package Sequelize-CLI. Create a project folder. In your project folder path, run the command below to initialize Sequelize in the folder.

How do you use destroy in Sequelize?

By default, Sequelize will assume the table uses an id column as its primary key. Now you can call destroy() from the Invoice model to delete a row by its id value: const count = await Invoice. destroy({ where: { id: 2 } }); console.


1 Answers

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'
}
like image 102
Catalyst Avatar answered Oct 08 '22 03:10

Catalyst