sequelize findOne return null 然后模型为空

sequelize findOne return null then model is empty

我有一个功能必须 return 配置域的活动。

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 因为我的 table 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