续集错误:包括意外.元素必须是模型、关联或对象



当我使用 get 请求调用我的 API 时收到错误:

Include unexpected. Element has to be either a Model, an Association or an object.

我的模型如下所示:

module.exports = (sequelize, Sequelize) => {
const Productions = sequelize.define("productions", {
id: {
type: Sequelize.SMALLINT,
autoIncrement: true,
primaryKey: true
},
setupTime: {
type: Sequelize.DECIMAL(6, 3)
},
notes: {
type: Sequelize.TEXT
}
}, { timestamps: false });
return Productions;
};
module.exports = (sequelize, Sequelize) => {
const ProductionPrints = sequelize.define("productionPrints", {
id: {
type: Sequelize.SMALLINT,
autoIncrement: true,
primaryKey: true
},
compDate: {
type: Sequelize.DATE
}
}, { timestamps: false });
return ProductionPrints;
};

模型之间的关系定义如下:

db.productions = require("./productions.model.js")(sequelize, Sequelize);
db.productionprints = require("./production-prints.model.js")(sequelize, Sequelize);
db.productions.hasOne(db.productionprints, {
foreignKey: {
name: 'productionId',
allowNull: false
}
});
db.productionprints.belongsTo(db.productions, { foreignKey: 'productionId' });

续集查询如下所示:

const db = require("../models");
const Productions = db.productions;
const ProductionPrints = db.productionPrints;
exports.findAll = (req, res) => {
Productions.findAll({
include: [ { model: ProductionPrints, as: 'prints' } ]
})
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "An error occurred while finding the productions."
});
});
};

我已经检查了其他人的问题,但对这些问题发布的任何解决方案都无济于事。通常,它是由拼写错误或所需路径中的错误引起的。我已经检查了这些以及我所有其他的工作,只是没有检查我在生产模型中包含的任何模型。

任何反馈将不胜感激。

错误是由拼写错误引起的:

db.productions = require("./productions.model.js")(sequelize, Sequelize);
db.productionprints = require("./production-prints.model.js")(sequelize, Sequelize);

当在分配给常量中引用它时:

const Productions = db.productions;
const ProductionPrints = db.productionPrints;

为我改变我的情况使用感到羞耻:

db.productionprints != db.productionPrints

我也有同样的问题,这通常是由命名问题引起的,要跟踪问题你可以检查以下地方之一来解决它

  1. 检查是否调用了正确的模型类名
  2. 导入模型时,请注意不要调用文件名而不是模型名称 =>导出的模型名称

3.通过调用导出的模型而不是文件名来检查关联是否正确

  1. 检查您的案例,例如用户与用户。

一个额外的提示是使用相同的名称作为模型和文件名以避免这些问题,因为当你使它们变得不同时,你可能会犯这些错误

按照Kelvin Nyadzayo的回答,我有model.findOne(options(方法,带有选项,如下所示include: [ { } ]在选项参数中

包含必须具有正确的语法:[{model: Model, as: 'assciationName'}]

矿井是空的

所以这触发了同样的错误

最新更新