node.js 仅从 Sequelize ORM 获取数据值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46380563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:29:37  来源:igfitidea点击:

Get only dataValues from Sequelize ORM

node.jssequelize.jssequelize-cli

提问by Gijo Varghese

I'm using the sequelize ORM to fetch data from a PSQL DB. However, when I retrieve something, a whole bunch of data is given. The only data I want is inside 'dataValues'. Of course, I can use object.dataValues. But, is there any other good solutions?

我正在使用 sequelize ORM 从 PSQL DB 获取数据。但是,当我检索某些东西时,会给出一大堆数据。我想要的唯一数据是在“dataValues”里面。当然,我可以使用 object.dataValues。但是,还有其他好的解决方案吗?

I'm using Sequelize 4.10

我正在使用 Sequelize 4.10

采纳答案by Gijo Varghese

The problem occurs only when I log it using:

仅当我使用以下方法记录时才会出现问题:

console.log(Model.findAll());

console.log(Model.findAll());

If I save it to a variable, I can directly access objects inside without using "dataValues"

如果我把它保存到一个变量中,我可以直接访问里面的对象,而不用使用“ dataValues

回答by Shivam

Yes you can

是的你可以

Model.findAll({
 raw: true,
 //Other parameters
});

would return just the data and not the model instance

将只返回数据而不是模型实例

回答by C Deuter

Sequelize wraps all it's return values in a virtual object that contains meta data. If you have an object and you just want the undecorated data values, you can unwrap them like so:

Sequelize 将所有返回值包装在包含元数据的虚拟对象中。如果你有一个对象并且你只想要未修饰的数据值,你可以像这样解开它们:

Model.findById(1).then(data => {
  console.log(data.get({ plain: true }));
});

Additionally if you just want to print out the object you can use the .toJSONmethod.

此外,如果您只想打印出对象,您可以使用该.toJSON方法。

Model.findById(1).then(data => {
  console.log(data.toJSON());
});

回答by Masoud Tavakkoli

Finally I found answer after searching a lot. you should do something like this

经过大量搜索,我终于找到了答案。你应该做这样的事情

const users = await db.users.findAll({})
   .map(el => el.get({ plain: true })) // add this line to code

source: github issue

来源:github问题

回答by defraggled

To clarify Masoud Tavakkoli's answer (which is not immediately clear on that github answer): using element.get({ plain: true })returns an array of objectswith each attribute key:value pairs.

为了澄清 Masoud Tavakkoli 的答案(在那个 github 答案上不是很清楚): usingelement.get({ plain: true })返回一个具有每个属性键:值对的对象数组。

If you just want an array of one specific attribute's values (eg user ids) instead of objects, you can use something like this:

如果您只想要一个包含特定属性值(例如用户 ID)而不是对象的数组,您可以使用以下内容:

const users = await User.findAll({
    attributes: ["id"], 
    where: {} // Your filters here
}).map(u => u.get("id")) // [1,2,3]

Nika Kasradze's answer actually achieves the same outcome as a middle-step; using the JSON stringifier generates the same array output. It's possible this is faster than mapping, but I'm not sure.

Nika Kasradze 的回答实际上达到了与中间步骤相同的结果;使用 JSON 字符串生成器生成相同的数组输出。这可能比映射更快,但我不确定。

const users = await User.findAll({
    attributes: ["id"], 
    where: {} // Your filters here
})
const userIds = JSON.stringify(users)) // [1,2,3]

回答by Nika Kasradze

For nested entities this stupid workaround works for me:

对于嵌套实体,这个愚蠢的解决方法对我有用:

let row = await Model.findOne({
     include: [ /*your includes here*/ ]
});
row = JSON.parse( JSON.stringify(row, null, 4) );

Somehow JSON.stringify(row, null, 4)removes all the extra stuff and pretends the dataValuesproperty of the object is the object itself. Then the JSON.parse(...)puts the object back together.

以某种方式JSON.stringify(row, null, 4)删除所有额外的东西并假装dataValues对象的属性是对象本身。然后JSON.parse(...)将对象放回原处。

EDIT:

编辑:

so apparently I'm new to typescript so I didn't need this at all. the console.log(row)printing huge object got me confused. I miss good ol' c# times :'(

所以显然我是打字稿的新手,所以我根本不需要这个。在console.log(row)打印巨物让我感到困惑。我想念好 ol' c# 时代 :'(