gpt4 book ai didi

sequelize.js - 修改 findOne 函数返回的数据

转载 作者:行者123 更新时间:2023-12-03 22:33:00 30 4
gpt4 key购买 nike

我创建了使用 db 获取配置的函数。我在我的服务器上使用了 Sequelize 。

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,
}
我尝试使用运算符 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
}
]
}

最佳答案

如果您希望从包含的模型中获取某些字段作为主模型的属性,您可以像这样将其添加到主 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,
},
},
],
});

关于sequelize.js - 修改 findOne 函数返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65052079/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com