mysql - Sequelize 错误 : Include unexpected. 元素必须是模型、关联或对象

标签 mysql node.js typescript sequelize.js

当我使用 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' });

sequelize 查询如下所示:
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

关于mysql - Sequelize 错误 : Include unexpected. 元素必须是模型、关联或对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62272169/

相关文章:

mysql - 将值附加到维基媒体中所有页面的末尾

javascript - 如何使用 express router 正确调用 passport.js 函数?

TypeScript 编译中缺少 Angular- 模块。请通过 'files' 或 'include' 属性确保它在您的 tsconfig 中

MySQL - 多表选择

php - 如何在 Laravel 中选择外键值

mysql - 没有从 mysql 查询中获取记录

javascript - 当尝试在 koajs 上使用 swig 渲染模板时,浏览器中的 "not found"纯文本

node.js - expressjs反转 Controller 中间件错误

javascript - 如何在 Angular 4 中将 observable<User> 转换为 Observable<boolean>

typescript - 如何将 import all 与其他的结合起来?