本文介绍了在Sequelize Model.create上将raw设置为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Sequelize上调用Model.create之后,我希望能够接收普通的原始对象,该对象本身是创建的,没有元数据或其他任何东西.就像Model.find中的{raw: true}选项一样.

I want to be able to receive the plain raw object after calling Model.create on Sequelize, the object itself that was created, no metadata or any other things. Just like the {raw: true} option in Model.find.

我已经看到了这个答案:将所有查询设置为raw = true sequelize ,不,Model.create({name: 'test'}, {raw: true})不起作用.

I've already seen this answer:Setting all queries to raw = true sequelize,and no, Model.create({name: 'test'}, {raw: true}) doesn't work.

谢谢

推荐答案

非常感谢您的帮助.我找到了一个解决方案,尽管这并不是我想要的,但是它可以正常工作,而且仍然很好.

Thank you very much for your help. I found a solution, though this isn't exactly what I'm looking for, but it works, and also still good.

sequelize实体具有.get()方法以返回纯对象版本.所以它是这样的:

The sequelize entity has a .get() method to return the plain object version. So it goes something like this:

Model.create(modelObject)
.then((resultEntity) => {
    const dataObj = resultEntity.get({plain:true})
}

来自此线程:将实体转换为普通对象,将其转换为普通对象.寻找CharlesA的答案.

Coming from this thread: Sequelize, convert entity to plain object. Look for CharlesA's answer.

没有尝试过使用数组,但是如果您对结果数组有疑问,请检查其注释及其旁边的答案.但是由于.create()仅返回一个对象,因此它对我有用.无论如何,如果使用的是.findAll(),则应使用{raw: true}选项而不是此解决方案,因为它可以在该方法中使用.

Haven't tried with arrays but check its comments and the answer next to it if you're having problems with array of results.But since .create() only returns an object, it works for me. Anyway if you are using .findAll(), you should use {raw: true} option instead of this solution because it works in that method.

P.S.如果有人仍然有一个解决方案,即Sequelize本身不会返回那个大的resultEntity对象,而只是返回普通数据对象,就像{raw: true}选项(因为我认为它还更轻巧?),我们就开放了.

P.S. If anyone still has a solution where Sequelize itself will not return that large resultEntity object, but just the plain data object, just like {raw: true} option (because I think that's still lighter?), we're open.

非常感谢您.

这篇关于在Sequelize Model.create上将raw设置为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:57