修改findOne函数返回的数据



我用db为get-config创建了函数。我在服务器上使用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,
},
},
],
});

最新更新