"TypeError: Cannot read properties of undefined (reading 'findAll')"



stack stack!!帮助这是模型:

const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Skill extends Model {
/**
* Helper method for defining associations.
* This method is not a part of DataTypes lifecycle.
* The `models/index` file will call this method automatically.
*/
// eslint-disable-next-line no-unused-vars
static associate(models) {
// define association here
}
}
Skill.init(
{
id:{
type: DataTypes.INTEGER,
allowNull: false,
primaryKey:true
},
s_name: {
type: DataTypes.STRING(255),
allowNull: false,
},
},
{
sequelize,
modelName: 'skill',
tableName: 'Skills'
}
);
return Skill;
};

and this is service

import {Skill} from "models";
class SkillService{
static async createNewSkill (data){
try {
const newskill = await Skill.create(data, {
fields: ["id", "s_name"],
});
return newskill;
} catch (error) {
throw new Error(error);
}
}
static async fetchAllSkills(){
try {
const users = await Skill.findAll({
order: [["id", "DESC"]],
attributes: { exclude: ["updatedAt"] },
});
return users;
} catch (error) {
throw new Error(error);
}
}
}
export default SkillService;

我已经尝试了很多事情,但我仍然得到一个错误。我正在使用的另一个模型与其服务一起正常工作。谁能帮我一下,谁能知道这个错误的来源。

您在模块中导入Skill,这很好,它的作用域可以在您的函数中访问

现在,如果你像这样在另一个模块中导入你的SkillService:import {SkillService} from ...那么就有问题了,因为如果你不使用require那么你只是导入那段代码,而不执行模块,因此你不会在你使用SkillService类的模块中导入Skill。通过调用它的静态方法,被导入的类不知道你在它的声明模块中的导入,因为你只从那里导入了类。

一个解决方案是使用export关键字导出类,并在模块中使用require函数来使用它。
另一个解决方案是在导入SkillService类的所有地方导入模型(未指明)

相关内容

最新更新