gpt4 book ai didi

node.js - Sequelize 预加载 - 返回更扁平的数据对象

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:37 24 4
gpt4 key购买 nike

鉴于 Sequelize 1.7.9 中的以下(工作)代码:

database.User.find({ 
where: { signupKey: signupKey },
include: [{ model: database.Company, as: "Company" }]
}).then(function( user ) {

我的user对象输出如下内容:

{ dataValues: 
{ id: 1,
email: 'email',
password: '',
firstname: null,
lastname: null,
companyRole: 'admin',
nonprofitRole: null,
signupKey: '24Pm9MZ22',
status: 'pending',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
CompanyId: 1,
NonprofitId: null,
company:
{ dataValues: [Object],
_previousDataValues: [Object],
__options: [Object],
options: [Object],
hasPrimaryKeys: true,
selectedValues: [Object],
__eagerlyLoadedAssociations: [],
isNewRecord: false } },
_previousDataValues:
{ id: 1,
email: 'email',
password: '',
firstname: null,
lastname: null,
companyRole: 'admin',
nonprofitRole: null,
signupKey: '24Pm9MZ22',
status: 'pending',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
CompanyId: 1,
NonprofitId: null,
company:
{ dataValues: [Object],
_previousDataValues: [Object],
__options: [Object],
options: [Object],
hasPrimaryKeys: true,
selectedValues: [Object],
__eagerlyLoadedAssociations: [],
isNewRecord: false } },
__options:
{ timestamps: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
instanceMethods: {},
classMethods: { associate: [Function] },
validate: {},
freezeTableName: false,
freezeAssociations: false,
underscored: false,
syncOnAssociation: true,
paranoid: false,
whereCollection: { signupKey: '24Pm9MZ22' },
schema: null,
schemaDelimiter: '',
language: 'en',
defaultScope: null,
scopes: null,
hooks: { beforeCreate: [], afterCreate: [] },
omitNull: false,
uniqueKeys: {},
hasPrimaryKeys: true },
options:
{ isNewRecord: false,
isDirty: false,
include: [ [Object] ],
includeNames: [ 'Company', 'company' ],
includeMap: { Company: [Object] },
includeValidated: true,
raw: true },
hasPrimaryKeys: true,
selectedValues:
{ id: 1,
email: 'email',
password: '',
firstname: null,
lastname: null,
companyRole: 'admin',
nonprofitRole: null,
signupKey: '24Pm9MZ22',
status: 'pending',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
CompanyId: 1,
NonprofitId: null },
__eagerlyLoadedAssociations: [],
isNewRecord: false,
company:
{ dataValues:
{ id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST) },
_previousDataValues:
{ id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST) },
__options:
{ timestamps: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
instanceMethods: {},
classMethods: [Object],
validate: {},
freezeTableName: false,
freezeAssociations: false,
underscored: false,
syncOnAssociation: true,
paranoid: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
language: 'en',
defaultScope: null,
scopes: null,
hooks: [Object],
omitNull: false,
uniqueKeys: {},
hasPrimaryKeys: true },
options:
{ isNewRecord: false,
isDirty: false,
include: undefined,
includeNames: undefined,
includeMap: undefined,
includeValidated: true,
raw: true },
hasPrimaryKeys: true,
selectedValues:
{ id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:14:29 GMT-0500 (EST) },
__eagerlyLoadedAssociations: [],
isNewRecord: false } }

在没有急切加载的正常sequelize下,我可以记录user.values并获得一个更正常的JS对象,除了急切加载,我在记录user.values时得到这个:

{ id: 1,
email: 'email',
password: 'password',
firstname: null,
lastname: null,
companyRole: 'admin',
nonprofitRole: null,
signupKey: 'bN2dGdNbD',
status: 'pending',
createdAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
CompanyId: 1,
NonprofitId: null,
company:
{ dataValues:
{ id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST) },
_previousDataValues:
{ id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST) },
__options:
{ timestamps: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
instanceMethods: {},
classMethods: [Object],
validate: {},
freezeTableName: false,
freezeAssociations: false,
underscored: false,
syncOnAssociation: true,
paranoid: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
language: 'en',
defaultScope: null,
scopes: null,
hooks: [Object],
omitNull: false,
uniqueKeys: {},
hasPrimaryKeys: true },
options:
{ isNewRecord: false,
isDirty: false,
include: undefined,
includeNames: undefined,
includeMap: undefined,
includeValidated: true,
raw: true },
hasPrimaryKeys: true,
selectedValues:
{ id: 1,
slug: 'company',
logo: null,
name: 'company',
createdAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST),
updatedAt: Fri Nov 21 2014 15:19:14 GMT-0500 (EST) },
__eagerlyLoadedAssociations: [],
isNewRecord: false } }

我希望能够运行 user.values 并获得一个平面 user 对象,就像返回的那样,并且让 user.company 也成为一个平面对象,作为 user 的子对象。 sequalize 是否提供了一种可以运行的方法,即使在急切加载的情况下,也可以让您获得像我所描述的那样组织的平面对象?

最佳答案

在 v3.9 中,您可以使用“get”方法将 ORM 对象转换为普通对象:

user.get({plain: true})

关于node.js - Sequelize 预加载 - 返回更扁平的数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27069648/

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