Sequelize :: findAll不是一个函数

时间:2019-12-16 05:29:32

标签: javascript node.js sequelize.js

下面是我的js文件。 我从模型中返回了一个对象,然后尝试将其导入我的控制器js文件。但这引发了我 admin_user.findAll不是函数的错误。

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-返回类型(序列化,数据类型)的admin_users

> 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;
};

我无法弄清楚为什么在以下情况下找不到findAll函数的原因  顺序定义。

我尝试了其他方法来导入模型,但是似乎没有任何效果。

请让我知道是否应该添加更多信息/代码以使该问题易于理解。

1 个答案:

答案 0 :(得分:0)

存在以下几行工作相关的问题:

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);

请参考以下有关postgres设置的内容: https://www.npmjs.com/package/sequelize-cli

这种方式的实现更加简洁并且易于维护