gpt4 book ai didi

mysql - 类型错误 : Cannot read property 'findAll' of undefined(sequelize)

转载 作者:行者123 更新时间:2023-11-30 21:27:45 34 4
gpt4 key购买 nike

我确实提到了类似的问题,例如 this但是在调试时,我得到的 models 是空的。我不知道为什么会这样。数据库表是 Users

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

模型/user.js:

'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 文件,但我在项目开始时评论了该文件,因为它给了我错误。我只是取消注释该文件并创建这样的模型 const models = require('../models/index');

所以应该需要 models/index.js 来获取 sequelize 和 DataTypes 实例,其中将使用模型,而不是 models/user.js。

关于mysql - 类型错误 : Cannot read property 'findAll' of undefined(sequelize),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58063041/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com