Mysql TypeError:无法读取属性';findAll';未定义(续集)的定义

Mysql TypeError:无法读取属性';findAll';未定义(续集)的定义,mysql,node.js,express,sequelize.js,Mysql,Node.js,Express,Sequelize.js,我确实提到过类似的问题,比如,但在这里调试时,我发现模型是空的。我不知道为什么会这样。DB表是用户 controllers/user.js: const models = require('../models'); const User = models.Users; exports.getAllUsers = (req,res,next) => { console.log(models); console.log(User); User.findAll({ attribute

我确实提到过类似的问题,比如,但在这里调试时,我发现
模型
是空的。我不知道为什么会这样。DB表是
用户

controllers/user.js:

const models = require('../models');
const User = models.Users;
exports.getAllUsers = (req,res,next) => {
  console.log(models);
  console.log(User);
User.findAll({
  attributes: attributesUser
}).then(users => {
  console.log(users);
  res.status(200).json({
    data: users
  });
});
}
'use strict';
module.exports = (sequelize, DataTypes) => {
  const Users = sequelize.define('Users', {
    firstName: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        len: [4, 255]
      }
    },
    middleName: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        len: [4, 255]
      }
    },
    lastName: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        len: [4,255]
      }
    },
    mobile: {
      type: DataTypes.STRING,
      validate:{
        len: [10,15],
        isNumeric:{
          msg:"Invalid Mobile Number"
        }
      }
    },
    address: {
      type: DataTypes.STRING,
      allowNull:false
    },
    dob: {
      type: DataTypes.DATEONLY,
      allowNull:false
    },
    email: {
      type: DataTypes.STRING,
      allowNull:false,
      unique: true,
      validate: {
        isEmail: true
      },
      set(email){
        this.setDataValue("email",email.toString().toLowerCase());
      }
    },
    password: {
      type: DataTypes.STRING,
      allowNull:false
    },
    gender: {
      type: DataTypes.STRING,
      allowNull:false
    },
    isDisabled: {
      type: DataTypes.BOOLEAN,
      allowNull: false
    },
    lastLoginTime: {
      type: DataTypes.DATE,
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull:false
    },
    updatedAt: {
      type: DataTypes.DATE,
    },
    deletedAt: {
      type: DataTypes.DATE,
    },
  }, {});
  Users.associate = function(models) {
    // associations can be defined here
    Users.belongsTo(models.UserType, {
      foreignKey: {
        name: "userTypeId",
        allowNull: false
      }
    });
    Users.belongsTo(models.Qualification, {
      foreignKey: {
        name: "qualificationId",
        allowNull: false
      }
    });
    Users.belongsTo(models.Organization, {
      foreignKey: "organizationId",
      allowNull: false
    });
  };
  return Users;
};
models/user.js:

const models = require('../models');
const User = models.Users;
exports.getAllUsers = (req,res,next) => {
  console.log(models);
  console.log(User);
User.findAll({
  attributes: attributesUser
}).then(users => {
  console.log(users);
  res.status(200).json({
    data: users
  });
});
}
'use strict';
module.exports = (sequelize, DataTypes) => {
  const Users = sequelize.define('Users', {
    firstName: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        len: [4, 255]
      }
    },
    middleName: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        len: [4, 255]
      }
    },
    lastName: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        len: [4,255]
      }
    },
    mobile: {
      type: DataTypes.STRING,
      validate:{
        len: [10,15],
        isNumeric:{
          msg:"Invalid Mobile Number"
        }
      }
    },
    address: {
      type: DataTypes.STRING,
      allowNull:false
    },
    dob: {
      type: DataTypes.DATEONLY,
      allowNull:false
    },
    email: {
      type: DataTypes.STRING,
      allowNull:false,
      unique: true,
      validate: {
        isEmail: true
      },
      set(email){
        this.setDataValue("email",email.toString().toLowerCase());
      }
    },
    password: {
      type: DataTypes.STRING,
      allowNull:false
    },
    gender: {
      type: DataTypes.STRING,
      allowNull:false
    },
    isDisabled: {
      type: DataTypes.BOOLEAN,
      allowNull: false
    },
    lastLoginTime: {
      type: DataTypes.DATE,
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull:false
    },
    updatedAt: {
      type: DataTypes.DATE,
    },
    deletedAt: {
      type: DataTypes.DATE,
    },
  }, {});
  Users.associate = function(models) {
    // associations can be defined here
    Users.belongsTo(models.UserType, {
      foreignKey: {
        name: "userTypeId",
        allowNull: false
      }
    });
    Users.belongsTo(models.Qualification, {
      foreignKey: {
        name: "qualificationId",
        allowNull: false
      }
    });
    Users.belongsTo(models.Organization, {
      foreignKey: "organizationId",
      allowNull: false
    });
  };
  return Users;
};
输出:

TypeError: Cannot read property 'findAll' of undefined
    at exports.getAllUsers (/mnt/part1/projects/Video_Coaching/backend-v2/controllers/user.js:87:10)
    at Layer.handle [as handle_request] (/mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/layer.js:95:5)
    at next (/mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/layer.js:95:5)
    at /mnt/part1/projects/Video_Coaching/backend-v2/node_modules/express/lib/router/index.js:281:22

问题是模型没有sequelize实例

虽然sequelize在models/下创建了一个index.js文件,但我在项目开始时对该文件进行了注释,因为它给了我错误。我刚刚取消了对该文件的注释,并创建了如下模型:
constmodels=require('../models/index')


因此,应该要求
models/index.js
获取sequelize和数据类型实例,而不是
models/user.js。

问题是模型没有sequelize实例

虽然sequelize在models/下创建了一个index.js文件,但我在项目开始时对该文件进行了注释,因为它给了我错误。我刚刚取消了对该文件的注释,并创建了如下模型:
constmodels=require('../models/index')


因此,您应该需要
models/index.js
来获取sequelize和DataTypes实例,在这里您将使用模型,而不是
models/user.js。

models.Users您正在使用的用户没有定义为module.exports在模型中是端点,而不是其函数中的“Users”。在控制器中定义用户时,请尝试使用models.user。是否可以发布
models.js
文件?我需要更多的信息来帮助你you@ShaahinShemshian我尝试了另外两种方法
const User=require('../models/User')
和你说的那一个,但两个错误都是一样的
@A.Granados
我已经根据你的要求更新了问题。请查看模型。您正在使用的用户未定义为模块。模型中的导出是端点,而不是其函数中的“用户”。在控制器中定义用户时,请尝试使用models.user。是否可以发布
models.js
文件?我需要更多的信息来帮助你you@ShaahinShemshian我尝试了另外两种方法
const User=require('../models/User')
和你说的那一个,但两个错误都是一样的
@A.Granados
我已经根据你的要求更新了问题。请看一看