首页 > 解决方案 > 修改 findOne 函数返回的数据

问题描述

我用 db 创建了 get config 函数。我在我的服务器上使用了续集。

async function getConfig(country) {
    return await db.config.findOne({
        where: { id: 1 },
        include: [
            {
                model: db.domain,
                attributes: ['domain'],
                as: 'domain',
                where: {
                    country_code: country,
                    isActive: true,
                },
            },
            {
                model: db.currency,
                attributes: ['id', 'code'],
            },
            {
                model: db.task,
                attributes: ['name'],
                as: 'name',
                where: {
                    isActive: true,
                },
            },
        ],
    });
}

这个函数返回一个结果。

"result": {
        "id": 1,
        "is_show": false,
        "version": "1.4.5",
        "domain": [
            {
                "domain": "https://....."
            }
        ],
        "currencies": [
            {
                "id": 1,
                "code": "USD",
                "symbol": "$",
            },
            {
                "id": 6,
                "code": "USD",
                "symbol": "$",
            }
        ],
        "name": [
            {
                "name": 123
            }
        ]
    }

我需要修改这个结果,我想得到这个结果

"result": {
        "id": 1,
        "is_show": false,
        "version": "1.4.5",
        "domain": "https://.....",  
        "currencies": [
            {
                "id": 1,
                "code": "USD",
                "symbol": "$",
            },
            {
                "id": 6,
                "code": "USD",
                "symbol": "$",
            }
        ],
        "name": 123,
    }

我尝试使用 operator 来做到这一点raw: true。但它没有成功。

           {
                model: db.task,
                attributes: ['name'],
                as: 'name',
                raw: true,
                where: {
                    isActive: true,
                },
            },

我该怎么做?


回复

"result": {
        "id": 1,
        "is_show": false,
        "version": "1.4.5",
        "domain": [
            {
                "0": "h",
                "1": "t",
                "2": "t",
                "3": "p",
                "4": "s",
                "5": ":",
                "6": "/",
                "7": "/",
                "8": ".",
                "9": ".",
                "10": ".",
                "11": ".",
                "12": "."
            }
        ],
        "currencies": [
            {
                "id": 1,
                "code": "USD",
                "symbol": "$",
            },
            {
                "id": 6,
                "code": "USD",
                "symbol": "$",
            }
        ],
        "name": [
            {
                "name": 123
            }
        ]
    }

标签: sequelize.js

解决方案


如果您希望从包含的模型中获取某些字段作为主模型的属性,您可以将其添加到主attributes选项中,如下所示(domain如下所示):

return await db.config.findOne({
        where: { id: 1 },
        attributes: { include: [[sequelize.col('domain.domain'), 'domain']] },
        include: [
            {
                model: db.domain,
                attributes: [],
                as: 'domain',
                where: {
                    country_code: country,
                    isActive: true,
                },
            },
            {
                model: db.currency,
                attributes: ['id', 'code'],
            },
            {
                model: db.task,
                attributes: ['name'],
                as: 'name',
                where: {
                    isActive: true,
                },
            },
        ],
    });

推荐阅读