gpt4 book ai didi

javascript - 如何使用 sequelize 获取用户 Angular 色?

转载 作者:行者123 更新时间:2023-12-03 22:16:42 26 4
gpt4 key购买 nike

我有一个这样的现实:

enter image description here

在我的解析器中,我正在获取这样的实体:

users: async () => {
const users = await db.user.findAll({
include: [
{
model: db.userroles,
include: [
{
model: db.roles,
attributes: ['Name'],
},
],
},
],
});

我记录了我从数据库中得到的信息:
 console.log('users are', users[0].dataValues.userroles[0].dataValues.role.Name); // logs "users are developer"

这向我表明获取了正确的 Angular 色。

我的 graphql 架构:
  type user {
Id: ID!
Email: String
RoleId: Int!
Password: String
ChangedPassword: Boolean
WeddingId: Int!
AttendantId: Int
role: [roles!]
}

type roles {
Id: ID!
Name: String!
}

在操场上,我发送了这个:
{users
{role
{Name}}
}

结果:
  "data": {
"users": [
{
"role": null
}
]
}
}

当我记录从数据库返回的整个用户对象时:
users are userroles {
dataValues:
{ UserId: 1,
RoleId: 1,
role:
roles {
dataValues: [Object],
_previousDataValues: [Object],
_changed: {},
_modelOptions: [Object],
_options: [Object],
isNewRecord: false } },
_previousDataValues:
{ UserId: 1,
RoleId: 1,
role:
roles {
dataValues: [Object],
_previousDataValues: [Object],
_changed: {},
_modelOptions: [Object],
_options: [Object],
isNewRecord: false } },
_changed: {},
_modelOptions:
{ timestamps: false,
validate: {},
freezeTableName: true,
underscored: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [],
name: { plural: 'userroles', singular: 'userrole' },
omitNull: false,
tableName: 'userroles',
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [MysqlDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: [Object] },
hooks: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
include: [ [Object] ],
includeNames: [ 'role' ],
includeMap: { role: [Object] },
includeValidated: true,
raw: true,
attributes: undefined },
isNewRecord: false,
role:
roles {
dataValues: { Name: 'Developer' },
_previousDataValues: { Name: 'Developer' },
_changed: {},
_modelOptions:
{ timestamps: false,
validate: {},
freezeTableName: true,
underscored: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [],
name: [Object],
omitNull: false,
tableName: 'roles',
sequelize: [Sequelize],
hooks: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
include: undefined,
includeNames: undefined,
includeMap: undefined,
includeValidated: true,
raw: true,
attributes: [Array] },
isNewRecord: false } }

我得到了我不知道如何呈现它们的 Angular 色?

我认为问题出在我的解析器中,我缺少另一组包含,但我不知道如何编写它。

最佳答案

userroles 与作为连接表的 userRoles 存在多对多关系。如果你还没有,应该在 user 上定义这个关系:

user.belongsToMany(roles, { through: userRoles })

这将让您直接使用 include Angular 色:
const users = await db.user.findAll({
include: [
db.roles,
],
});

关于javascript - 如何使用 sequelize 获取用户 Angular 色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61205964/

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