0
votes

I'm making an application with ORM sequelize for the first time, I got to the model part, but when I run it it points me to an error that I can't solve, if anyone knows or can help me, thank you in advance, I'll leave the model code below and the error message

const Model = require('sequelize');
const DataTypes = require('sequelize');

class User extends Model {
  static init(sequelize) {
    super.init({
      name: DataTypes.STRING,
      password: DataTypes.INTEGER,
      email: DataTypes.STRING,
    }, {
      sequelize
    })
  }
}

module.exports = User;

this is the error message: super.init({ ^

TypeError: (intermediate value).init is not a function at Function.init (/home/lagos/Documents/MyProjectSGBD/api/src/models/User.js:7:11) at Object. (/home/lagos/Documents/MyProjectSGBD/api/src/database/index.js:8:6) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) at Object. (/home/lagos/Documents/MyProjectSGBD/api/src/app.js:4:1) [nodemon] app crashed - waiting for file changes before starting...

1
If my answer solved your problem, please accept and upvote it. - Tarun Chawla

1 Answers

1
votes

I think your imports are not correct. It should be

const { Sequelize, DataTypes, Model } = require('sequelize');

Also, why not define models with sequelize.define. If you are defining them through extending Model, then try this

const { Sequelize, DataTypes, Model } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory:');

class User extends Model {}

User.init({
  name: DataTypes.STRING,
  password: DataTypes.INTEGER,
  email: DataTypes.STRING,
},{
  sequelize
});

console.log(User === sequelize.models.User); // true

Reference -> https://sequelize.org/master/manual/model-basics.html