错误 [ExceptionsHandler] 无法读取未定义的属性(读取 'filter')

ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'filter')

我是嵌套js的新手。所以我在这里要做的是一个用户只有一个角色。所以我必须创建一个端点作为 users/:id/link-role

角色实体

import { Company } from 'src/company/entities/company.entity';
import { Entitlement } from 'src/entitlement/entities/entitlement.entity';
import { BaseTable } from 'src/shared/base-table';
import { User } from 'src/user/entities/user.entity';
import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
} from 'typeorm';
import { RoleType } from '../enum/roles.enum';

@Entity()
export class Role extends BaseTable {
  @Column({
    type: 'enum',
    enum: RoleType,
  })
  roles: RoleType;

  @Column({ name: 'description' })
  description: string;
  entitlement: any;

  @ManyToMany(() => Entitlement, { eager: true })
  @JoinTable({
    name: 'role-entitlements',
    joinColumn: {
      name: 'role_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'entitlement_id',
      referencedColumnName: 'id',
    },
  })
  entitlements: Entitlement[];

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  users: User[];

  @ManyToOne(() => Company, (company) => company.roles)
  @JoinColumn({ name: 'company_id' })
  company: Company;
}

用户实体

import { Company } from 'src/company/entities/company.entity';
import { NegativeEntitlement } from 'src/entitlement/entities/negative-entitlement.entity';
import { PositiveEntitlement } from 'src/entitlement/entities/positive-entitlement.entity';
import { BaseTable } from 'src/shared/base-table';
import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  OneToMany,
} from 'typeorm';

@Entity()
export class User extends BaseTable {
  @Column({ name: 'first_name', length: 100 })
  firstName: string;

  @Column({ name: 'middle_name', length: 100 })
  middleName: string;

  @Column({ name: 'last_name', length: 100 })
  lastName: string;

  @Column({ name: 'preferred_name', length: 100 })
  preferredName: string;

  @Column({ name: 'email', length: 320 })
  email: string;

  @Column({ name: 'phone', length: 20 })
  phone: string;

  @ManyToMany(() => Company)
  @JoinTable({
    name: 'user_companies',
    joinColumn: {
      name: 'user_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'company_id',
      referencedColumnName: 'id',
    },
  })
  companies: Company[];

  @OneToMany(
    () => PositiveEntitlement,
    (positiveEntitlements) => positiveEntitlements.user,
  )
  positiveEntitlements: PositiveEntitlement[];

  @OneToMany(
    () => NegativeEntitlement,
    (negativeEntitlements) => negativeEntitlements.user,
  )
  negativeEntitlements: NegativeEntitlement[];
}

现在,在用户服务中,我创建了一个函数来获取用户和角色,在 return 中将角色链接到用户

async addRole(id: string, addRoleDto: AddRoleDto) {
    const currUser: User = await this.findAndValidateUser(id);
    console.log('currentUser: ', currUser);
    const role: Role = await this.rolesService.findAndValidateRole(
      addRoleDto.roleId,
    );
    console.log('role:', role);
    console.log('users: ', role.users);
    const users: User[] = role.users.filter((user) => user.id == currUser.id);

    if (users.length > 0) {
      return new CustomException('user already has a role');
    }
    role.users = role.users.concat(currUser);
    await this.rolesRepository.save(role);
  }

在上面的函数中,我试图查找用户是否已经拥有该特定角色(如果为真)return 异常(如果为假)则将当前用户连接到数组。 如上所述,我收到以下错误。我无法理解问题出在哪里。

TypeORM 默认不加载关系,您要么必须在查询数据时指定它们,要么将关系标记为 eager,这将导致每次都自动加载它们。

在您的例子中,usersRole 中的 ManyToOne 关系。我想象在 roleService.findAndValidateRole 中的某个地方,您可以通过这样做来加载角色:

const role = await Role.findOne(roleId, { 
  relations: 'users' // add this
});

您也可以将关系标记为 eager,但请注意它每次都会加载用户,您可能不希望这样。如果你到处都有急切关系,你的应用程序会通过加载不必要的数据给数据库带来很大压力,你的应用程序最终会变得非常慢。

@Entity()
export class Role extends BaseTable {
  // ...

  @ManyToOne(() => User, { eager: true }) // add this
  @JoinColumn({ name: 'user_id' })
  users: User[];

  // ...
}