0

I'm using a model created by sequelice-cli, but when I use it, I get an error message.

I am using an ORM (sequelize) and when I try to access the methods of the model, for example finOne(), findAll(), update(), it does not recognize it and throws an error, I already tried in another way that it works, but it does not is the right one, I am using sequelize-cli to generate the models

I need your help please!!!

This is my model:

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class scholarship_student extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  scholarship_student.init({
    id: DataTypes.INTEGER,
    name: DataTypes.STRING,
    last_name: DataTypes.STRING,
    status: DataTypes.ENUM("Activo", "Inactivo")
  }, {
    sequelize,
    modelName: 'scholarship_student',
  });


  return scholarship_student;
};

and this is my controller

const becadosController = {};
const db = require("../models/index");
const validation = require("../validation");
const scholarship_student = require("../models/scholarship_student");

becadosController.get = async (req, res) => {

  await scholarship_student.findAll()
    .then((student) => {
      res.json(student);
    })
    .catch((err) => {
      console.log(err);
      res.sendStatus(500);
    });
};

the error:

enter image description here

ChrisDLH
  • 123
  • 1
  • 2
  • 9

1 Answers1

0

You have to setup a connection config that then you will pass as a parameters to your model instance. Example:

const connection = new Sequelize(
  yourDatabaseName,
  yourDatabaseUsername,
  yourDatabasePassword,
  {
    host: yourDatabaseHost,
    dialect: 'whateverDialectYouAreUsing', // example 'mssql'
  }
)

and then

await scholarship_student(connection).findAll()
  .then((student) => {
    res.json(student);
  })
  .catch((err) => {
    console.log(err);
    res.sendStatus(500);
  });