简体   繁体   English

findTrees 方法在 Nest JS 中不起作用

[英]findTrees method does not work in Nest JS

I've got a problem using tree entity.我在使用树实体时遇到问题。 I'm using typeORM with nestjs.我在 nestjs 中使用 typeORM。 My entity is this:我的实体是这样的:

@Entity()
@Tree('closure-table')
export class PermissionEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: true, unique: true })
  key: string;

  @Column({ nullable: true })
  displayName?: string;

  @TreeChildren()
  children: PermissionEntity[];

  @TreeParent()
  parent: PermissionEntity;
}

In my module I added the entity this way:在我的模块中,我以这种方式添加了实体:

@Module({
  imports: [
    UsersModule,
    RolesModule,
    TypeOrmModule.forFeature([PermissionEntity]),
  ],
  providers: [
    {
      provide: 'PERMISSION_SERVICE',
      useClass: PermissionsService,
    },
    {
      provide: 'APP_GUARD',
      useClass: JwtAuthGuard,
    },
  ],
  controllers: [PermissionsController],
})
export class PermissionsModule {}

The codes below is my service file:下面的代码是我的服务文件:

export class PermissionsService {
  constructor(
    @InjectRepository(PermissionEntity)
    private readonly permissionRepository: TreeRepository<PermissionEntity>,
    @Inject('USER_SERVICE') private readonly userService: UsersService,
    @Inject('ROLES_SERVICE') private readonly rolesService: RolesService,
  ) {}

  async create(registerPermissionDto: RegisterPermissionDto) {
    this.permissionRepository.create(registerPermissionDto);
    return this.permissionRepository.save(registerPermissionDto);
  }

  async getUserPermissions(userId: number, ownerId: number) {
    return this.permissionRepository.findTrees();
  }

}

When getUserPermissions() service is called this error occures in console:当调用 getUserPermissions() 服务时,控制台中会出现此错误:

[Nest] 10644 - 08/12/2022, 8:15:44 PM ERROR [ExceptionsHandler] this.permissionRepository.findTrees is not a function [Nest] 10644 - 08/12/2022,晚上 8:15:44 错误 [ExceptionsHandler] this.permissionRepository.findTrees 不是 function

I've searched every where and I could not succeed in finding a solution?我到处搜索,但找不到解决方案? Is there a bug with nestJs and typeORM Tree entity? nestJs 和 typeORM Tree 实体是否存在错误? Or do we have working example ?或者我们有工作示例吗?

use like this:像这样使用:

first inject this首先注入这个

@InjectDataSource() 
private readonly dataSource: DataSource,

then:然后:

this.dataSource.getTreeRepository(PermissionEntity).findTrees();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM