Sequelize.js sequelize findOne返回null,则模型为空

Sequelize.js sequelize findOne返回null,则模型为空,sequelize.js,Sequelize.js,我有一个函数,它必须返回带有域活动的配置 async function getConfig(configId, country) { return await db.config.findOne({ where: { id: configId }, include: [ { model: db.domain, attributes: ['id', 'domain'],

我有一个函数,它必须返回带有域活动的配置

async function getConfig(configId, country) {
    return await db.config.findOne({
        where: { id: configId },
        include: [
            {
                model: db.domain,
                attributes: ['id', 'domain'],
                where: {
                    isActive: true,
                },
            },
        ],
    });
}
但我得到了一个默认错误-TypeError:无法读取未定义的属性'dataValues',因为我的表域为空,但我想得到结果

    "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,那么它意味着将内部联接转换为左外部联接。

您只需指示required:false选项,如下所示:

return await db.config.findOne({
        where: { id: configId },
        include: [
            {
                required: false,
                model: db.domain,
                attributes: ['id', 'domain'],
                where: {
                    isActive: true,
                },
            },
        ],
    });
通过这种方式,您可以说Sequelize希望获得主模型实例,而不考虑关联模型中的条件


如果您熟悉SQL,那么它意味着将内部联接转换为左外部联接。

您可以包括模型定义吗?您使用了错误的关联类型。您也不必返回wait,因为它是一个异步函数,所以您可以只返回承诺。@doublesharp我添加了我的两个模型。您的关联实际上很好,Anatoly关于将required:false添加到include的回答是正确的。您可以包括模型定义吗?您使用了错误的关联类型。您也不必返回wait,因为它是一个异步函数,所以您可以只返回承诺。@doublesharp我添加了我的两个模型。您的关联实际上很好,Anatoly关于添加required:false到include的回答是正确的。