Reet Pratayay
Reet Pratayay

Reputation: 21

Sequelize:: findAll is not a function

below are my js files. I've returned an object from the model, and then trying to import it my controller js file. but it is throwing me the error that admin_user.findAll is not a function.

helloController.js

var DataTypes = require("sequelize/lib/data-types");
const dbconconfig = require("../dbseq.config.js").db;
var admin_users = require("../models/admin_users");
var admin_roles = require("../models/admin_roles");
const logger = require("tracer").colorConsole();
admin_roles = new sequelize(dbconconfig);
admin_users = new sequelize(dbconconfig);


async sendDB(req, res) {
    logger.info("db called");
    admin_users
      .findAll({
        include: [{ model: admin_roles }]
      })
      .then(admin_users => {
        const resObj = admin_users.map(admin_users => {
          return Object.assign(
            {}

Admin_user.js - which is returning admin_users of type (sequelize, DataTypes)

> admin_users.js



    module.exports = function(sequelize, DataTypes) {
  const admin_users = sequelize.define(
    "adminUsers",
    {
      id: {
        type: DataTypes.INTEGER(22),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
        field: "id"
      },
      countryCode: {
        type: DataTypes.STRING(10),
        allowNull: true,
        defaultValue: "+91",
        field: "country_code"
      },
      mobileNo: {
        type: DataTypes.STRING(20),
        allowNull: false,
        field: "mobile_no"
      },
      email: {
        type: DataTypes.STRING(128),
        allowNull: true,
        field: "email"
      },
      firstName: {
        type: DataTypes.STRING(128),
        allowNull: true,
        field: "first_name"
      },
      lastName: {
        type: DataTypes.STRING(128),
        allowNull: true,
        field: "last_name"
      },
      lockVersion: {
        type: DataTypes.INTEGER(11),
        allowNull: true,
        defaultValue: "0",
        field: "lock_version"
      },
      blocked: {
        type: DataTypes.INTEGER(1),
        allowNull: true,
        defaultValue: "0",
        field: "blocked"
      },
      deleted: {
        type: DataTypes.INTEGER(1),
        allowNull: true,
        defaultValue: "0",
        field: "deleted"
      },
      createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: sequelize.literal("CURRENT_TIMESTAMP"),
        field: "created_at"
      },
      updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: sequelize.literal("CURRENT_TIMESTAMP"),
        field: "updated_at"
      },
      password: {
        type: DataTypes.STRING(128),
        allowNull: false,
        field: "password"
      },
      role: {
        type: DataTypes.STRING(128),
        allowNull: true,
        field: "role"
      }
    },
    {
      tableName: "admin_users"
    }
  );

  adminUser.associate = models => {
    adminUser.belongsTo(models.adminRoles, {
      foreignKey: "id"
    });
  };
  return admin_users;
};

I am not able to figure out why it's not able to find the findAll function when defined in sequelize.

i tried other methods to import the models, but nothing seems to be working.

please let me know if I should add more info/code for making this question comprehensible.

Upvotes: 1

Views: 574

Answers (1)

shubham
shubham

Reputation: 449

there is issue associated with how the following lines work:

const dbconconfig = require("../dbseq.config.js").db;
var admin_users = require("../models/admin_users");
var admin_roles = require("../models/admin_roles");
admin_roles = new sequelize(dbconconfig);//You cannot have new sequelize
admin_users = new sequelize(dbconconfig);

Please refer the fllowing for your postgres setup: https://www.npmjs.com/package/sequelize-cli

The implementation is much cleaner this way and easy to maintain

Upvotes: 0

Related Questions