javascript - Sequelize 请求错误 SequelizeEagerLoadingError

标签 javascript node.js sequelize.js

我是 sequelize 的新手 我正在尝试使用关联表提出请求

我有一个名为 Experience 的第一个模型

module.exports = function (sequelize, DataTypes) {
    const Experience = sequelize.define('experience', {
        internalId: {
            type: DataTypes.BIGINT,
            unique: true,
            allowNull: false,
        },
        label: {
            type: DataTypes.STRING,
            unique: false,
            allowNull: false,
        },
        picture: {
            type: DataTypes.TEXT,
            unique: false,
            allowNull: true,
        },
        type: {
            type: DataTypes.STRING,
            validate: {
                isIn: {
                    args: [[
                        'generic',
                        'specific',
                    ]],
                    msg: 'Must be a valid type',
                },
            },
            unique: false,
            allowNull: true,
        },
        author: {
            type: DataTypes.STRING,
            unique: false,
            allowNull: true,
            defaultValue: 'import',
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            defaultValue: true,
        },
    });

    Experience.associate = (models) => {
        Experience.belongsToMany(models.Tag, {
            as: 'Tags',
            through: models.ExperienceTag,
        });
    };

    return Experience;
};

第二个叫做 Tags
module.exports = function (sequelize, DataTypes) {
    const Tag = sequelize.define('tag', {
        internalId: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false,
        },
        name: {
            type: DataTypes.STRING,
            unique: false,
            allowNull: false,
        },
        author: {
            type: DataTypes.STRING,
            unique: false,
            allowNull: true,
            defaultValue: 'import',
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            defaultValue: true,
        },
    });

    Tag.associate = (models) => {
        Tag.belongsToMany(models.Experience, {
            as: 'Experiences',
            through: models.ExperienceTag,
        });
    };

    return Tag;
};

关联表名称为 ExperienceTags我想要所有拥有 ExperiencetagId = 44
这是我的要求:
Experience.findAll({
        include: [{
            model: ExperienceTag,
            where: { tagId: 44 },
        }],
    })
        .then((results) => {
            winston.warn(JSON.stringify(results, null, 2));
            res.status(200)
                .send(results);
        })
        .catch(error => res.status(500)
            .send({ error: error.toString() }));

但是当我执行它时,我有一个错误,如:
{
  "error": "SequelizeEagerLoadingError: experienceTag is not associated to experience!"
}

最佳答案

我认为您喜欢包含 Tag 而不是 ExperienceTag ,以下示例可能对您有所帮助

Experience.findAll({
        include: [{
            model: Tag, //model name which you want to include
            as: 'Tags', // you have to pass alias as you used while defining
            where: { tagId: 44 },
        }],
    })

关于javascript - Sequelize 请求错误 SequelizeEagerLoadingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52365040/

相关文章:

javascript - 等到所有 promise 都在 JavaScript 中解决

javascript - 编程语言中变量提升的优点和缺点是什么?

javascript - Canvas 绘制图像不工作

node.js - 如何在运行时获取nodejs可执行文件的路径

mysql - 使用 sequelize 多次更新同一行的正确方法

javascript - 动态调整 jqGrid 添加/编辑表单字段的大小

javascript - 与 Webpack 捆绑后的模块功能无法识别

node.js - 如何按顺序运行多个 Node 流?

javascript - 如何在 Sequelize.js + Postgres.js 中给出主键和外键关系

javascript - 在 sequelize 范围内包含一对多关联