0

This is my user.js Sequelize class:

'use strict';
const {Sequelize} = require('sequelize');
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class User 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 init(sequelize) {
        super.init(
        {
          name: Sequelize.STRING,
        },
        {
          sequelize,
        });
        this.addHook('beforeSave', async (user) => {
          return user.id = uuid();
        });
        return this;
      }
      static associate(models) {
        // define association here
        User.hasMany(UserRole,
          {
            foreignKey: {
              field:'UserId',
              allowNull: false,
            },
          });
      }
  }
  User.init({
    Id: DataTypes.UUID,
    Name: DataTypes.STRING,
    UserName: DataTypes.STRING,
    Email: DataTypes.STRING,
    Password: DataTypes.STRING,
    PhoneNumber: DataTypes.STRING,
    MobileNumber: DataTypes.STRING,
    DateOfBirth: DataTypes.DATE,
    LockoutEnabled: DataTypes.BOOLEAN,
    LockoutEnd: DataTypes.DATE
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};

I am trying to declare an instance to do a findAll call to my db.

So far this is what I have and it keeps saying findAll is not a function:

const {Sequelize} = require('sequelize');
const {userModel}= require('../../models/user');


function GetAll(){
  return user.findAll();
}



module.exports = {
  GetAll,
}

I'm new to Express JS and even more so for Sequelize.

halfer
  • 19,824
  • 17
  • 99
  • 186
JianYA
  • 2,750
  • 8
  • 60
  • 136

1 Answers1

0

Look at the question and my answer here to figure out how to register models and associations.
You export a function that returns a registered model, so you need to call it first passing a Sequelize instance and DataTypes and then you can use the returned model to call a method like findAll. It's better to register all models first and only after that register all associations between them (see the mentioned link).

Anatoly
  • 20,799
  • 3
  • 28
  • 42