gpt4 book ai didi

javascript - 在 Sequelize ORM 中只返回没有字段名的数据

转载 作者:行者123 更新时间:2023-12-03 22:17:25 24 4
gpt4 key购买 nike

我正在使用 Sequelize ORM 从数据库中获取数据

.findAll({
attributes: ['value'],
where: {
code: { [Op.like]: 'images_default_droplet_%' },
is_published: 1
}
})

它像这样返回:
{"value":"data"}

我可以只获取没有字段名称的数据吗?

返回我想要的:
{"data"}

最佳答案

不,{"data"} 不是有效的 JSON 结构。您可以使用 Array.map() 将其转换为“数据”数组;将 raw: true 传递给 findAll() 将返回一个纯 JSON 对象数组,这比在将它们映射到您想要的值之前先转换为模型实例更有效。

const results = await Model.findAll({
attributes: ['value'],
where: {
code: { [Op.like]: 'images_default_droplet_%' },
is_published: 1
},
raw: true, // return array of plain JSON objects
});

// 'datas' is an array of ["data","data","data"...]
const datas = results.map((result) => result.value);

关于javascript - 在 Sequelize ORM 中只返回没有字段名的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53978802/

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