Error: Nodejs, javascript "belongsTo called with Something that’s not a subclass of Sequelize.Model"

Asked

Viewed 568 times

0

I am trying to use belongsTo function and get this error.

Connection DATABASE has been established successfully.
(node:14119) UnhandledPromiseRejectionWarning: Error: User.belongsTo called with something that's not a subclass of Sequelize.Model
    at Function.<anonymous> (/home/pedro/Documentos/projetos/node/gostack/goBarber/node_modules/sequelize/lib/associations/mixin.js:93:13)
    at Function.associate (/home/pedro/Documentos/projetos/node/gostack/goBarber/src/app/models/User.js:38:10)
    at models.map.map (/home/pedro/Documentos/projetos/node/gostack/goBarber/src/database/index.js:25:48)
    at Array.map (<anonymous>)
    at Database.init (/home/pedro/Documentos/projetos/node/gostack/goBarber/src/database/index.js:25:8)
(node:14119) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14119) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

model/User

import Sequelize, { Model } from 'sequelize';
import bcrypt from 'bcryptjs';

const saltRounds = 4;

/* virtual significa que este campo nunca vai existir */


class User extends Model {
  static init(sequelize) {
    super.init({
      name: Sequelize.STRING,
      email: Sequelize.STRING,
      phone: Sequelize.STRING,
      password: Sequelize.VIRTUAL,
      password_hash: Sequelize.STRING,
      provider: Sequelize.BOOLEAN,
      dd: Sequelize.STRING,
    },
    {
      sequelize,
    });

    /* addHook é uma propiedade do sequelize para executar uma bloco de código em uma
     * operação seja ela antes depois etc */

    this.addHook('beforeSave', async (user) => {
      if (user.password) {
        user.password_hash = await bcrypt.hash(user.password, saltRounds);
      }
    });
    return this;
  }

  /* Associa o avatar_id do model file com o model usuario */

  static associate(models) {
    this.belongsTo(models.File, { foreignKey: 'avatar_id' });
  }
  /* Dentro do model pode fazer funções referentes a ele, muito interessante */

  checkPassword(password) {
    return bcrypt.compare(password, this.password_hash);
  }
}

export default User;

and then enter the index

import Sequelize from 'sequelize';
import DbConfig from '../config/database';
import User from '../app/models/User';
import File from '../app/models/File';

const models = [User, File];

class Database {
  constructor() {
    this.init();
  }

  async init() {
    this.connection = new Sequelize(DbConfig);

    try {
      await this.connection.authenticate();
      console.log('Connection DATABASE has been established successfully.');
    } catch (err) {
      console.error('connection to database ERROR ERROR ERROR:', err);
    }

    models
      .map((model) => model.init(this.connection))
      .map((model) => model.associate && model.associate(this.connection.model));
  }
}

export default new Database();

1 answer

0


For some mysterious reason it was just changing the word 'model' inside the map for any other thing that works example.

models
  .map((exemplo) => model.init(this.connection))
  .map((exemplo) => model.associate && model.associate(this.connection.model));
  • Are you sure it worked? Is it associating? Did you see if there is a model and its value? It wasn’t a mysterious reason, you might have overwritten the model variable in the map

Browser other questions tagged

You are not signed in. Login or sign up in order to post.