sequelize.js - Sequelize findOne 返回 null 然后模型为空

标签 sequelize.js

我有一个函数,它必须返回包含域事件的配置。

async function getConfig(configId, country) {
    return await db.config.findOne({
        where: { id: configId },
        include: [
            {
                model: db.domain,
                attributes: ['id', 'domain'],
                where: {
                    isActive: true,
                },
            },
        ],
    });
}
但是我收到错误 default - TypeError: Cannot read property 'dataValues' of undefined 因为我的表 domain 是空的但我想得到结果
    "result": {
        "id": 4,
        "country": "US",
        "domains": []
    }
我该怎么做?
配置模型

const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
    class Config 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.
         */
        static associate(models) {
            Config.hasMany(models.domain, {
                foreignKey: 'configId',
                onDelete: 'CASCADE',
            });
        }
    }

    Config.init(
        {
            id: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: DataTypes.INTEGER,
            },
            country: {
                type: DataTypes.STRING,
                allowNull: false,
            },
        },
        {
            sequelize,
            tableName: 'configs',
            modelName: 'config',
            timestamps: false,
        },
    );
    return Config;
};
领域模型
const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
    class Domain 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.
         */
        static associate(models) {
            Domain.belongsTo(models.config, {
                foreignKey: 'configId',
            });
        }
    }

    Domain.init(
        {
            id: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: DataTypes.INTEGER,
            },
            domain: {
                type: DataTypes.STRING,
                allowNull: false,
            },
            isActive: {
                type: DataTypes.BOOLEAN,
                allowNull: false,
                defaultValue: false,
            },
        },
        {
            sequelize,
            tableName: 'domains',
            modelName: 'domain',
            timestamps: false,
        },
    );
    return Domain;
};

最佳答案

您只需要像这样指示 required: false 选项:

return await db.config.findOne({
        where: { id: configId },
        include: [
            {
                required: false,
                model: db.domain,
                attributes: ['id', 'domain'],
                where: {
                    isActive: true,
                },
            },
        ],
    });
这样你就对 Sequelize 说你希望获得主模型实例,而不管关联模型中的条件如何。
如果您熟悉 SQL,则意味着将 INNER JOIN 转换为 LEFT OUTER JOIN

关于sequelize.js - Sequelize findOne 返回 null 然后模型为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65093503/

相关文章:

ajax - ExpressJS 后端挂起太多请求

javascript - 为什么 save() 在 Sequelize 中不起作用?

mysql - Sequelize 获取属性中的关联模型数据

node.js - 如何先设置限制而不是创建 where 查询?

node.js - 无法使用 Node.js 在 heroku 上创建 postgresql 表

node.js - 使用 PostgreSQL 的 SequelizeDatabaseError UPDATE

javascript - Sequelize 动态数据库配置

javascript - 模型中的函数接收消息 ** 不是函数 **

json - 使用 sequelize 防止将连接表数据添加到 json

node.js - Sequelize : how to add one more column to primary key formed by association?