NestJS/TypeORM: 无法设置只有 getter 的 #<Repository> 的 属性 元数据

NestJS/TypeORM: Cannot set property metadata of #<Repository> which has only a getter

我尝试 运行 我的 nestjstutorial 应用程序,显示以下错误。我的后端连接到 PostgreSQL 数据库。

TypeError: Cannot set property metadata of # which has only a getter at EntityManager.getCustomRepository (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\src\entity-manager\EntityManager.ts:1404:59) at DataSource.getCustomRepository (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\src\data-source\DataSource.ts:465:29) at InstanceWrapper.useFactory [as metatype] (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\node_modules@nestjs\typeorm\dist\typeorm.providers.js:13:35) at Injector.instantiateClass (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\node_modules@nestjs\core\injector\injector.js:333:55) at callback (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\node_modules@nestjs\core\injector\injector.js:48:41) at processTicksAndRejections (node:internal/process/task_queues:96:5) at Injector.resolveConstructorParams (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\node_modules@nestjs\core\injector\injector.js:122:24) at Injector.loadInstance (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\node_modules@nestjs\core\injector\injector.js:52:9) at Injector.loadProvider (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\node_modules@nestjs\core\injector\injector.js:74:9) at Injector.lookupComponentInImports (D:\Ganesh\MyDrive\nestjs\nestjs_tutorial\nestjsturorial\node_modules@nestjs\core\injector\injector.js:265:17)

我的代码如下所示:

app.module

    import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { UserController } from './user/user.controller';
    import { UserModule } from './user/user.module';
    import { UserService } from './user/user.services';
    import { QuizModule } from './modules/quiz/quiz.module';
    //import { QuizController } from './modules/quiz/quiz.controller';
    //import { QuizService } from './modules/quiz/quiz.services';
    import { TypeOrmModule } from '@nestjs/typeorm';
    import { typeOrmConfig } from './config/typeorm.config';
    //import { QuizRepository } from './modules/quiz/quiz.repository';
    
    @Module({
      imports: [UserModule, QuizModule, TypeOrmModule.forRoot(typeOrmConfig)],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {}

quiz.controller

    import {
      Body,
      Controller,
      Get,
      HttpCode,
      Post,
      UsePipes,
      ValidationPipe,
    } from '@nestjs/common';
    import { QuizService } from './quiz.services';
    import { CreateQuizDto } from '../dto/CreateQuiz.dto';
    
    @Controller('quiz')
    export class QuizController {
      constructor(private readonly quizService: QuizService) {}
    
      @Get('/')
      getAllQuiz() {
        return this.quizService.getAllQuiz();
      }
    
      @Post('/create')
      @HttpCode(200)
      @UsePipes(ValidationPipe)
      async createQuiz(@Body() quizData: CreateQuizDto) {
        return await this.quizService.createNewQuiz(quizData);
      }
    }

quiz.services

    import { Injectable } from '@nestjs/common';
    import { InjectRepository } from '@nestjs/typeorm';
    import { QuizRepository } from './quiz.repository';
    import { CreateQuizDto } from '../dto/CreateQuiz.dto';
    
    @Injectable()
    export class QuizService {
      constructor(
        @InjectRepository(QuizRepository) private quizRepository: QuizRepository,
      ) {}
    
      getAllQuiz() {
        return [1, 2, 'from service', 3];
      }
      async createNewQuiz(quiz: CreateQuizDto) {
        return await this.quizRepository.save(quiz);
      }
    }

quiz.module

    import { Module } from '@nestjs/common';
    import { TypeOrmModule } from '@nestjs/typeorm';
    import { QuizController } from './quiz.controller';
    import { QuizService } from './quiz.services';
    import { QuizRepository } from './quiz.repository';
    
    @Module({
      controllers: [QuizController],
      imports: [TypeOrmModule.forFeature([QuizRepository])],
      providers: [QuizService],
    })
    export class QuizModule {}

quiz.repository

    import { EntityRepository, Repository } from 'typeorm';
    import { Quiz } from './quiz.entity';
    
    @EntityRepository(Quiz)
    export class QuizRepository extends Repository<Quiz> {
      //<Quiz> means Quiz module
    }

typeorm.config

import { TypeOrmModuleOptions } from '@nestjs/typeorm';

    export const typeOrmConfig: TypeOrmModuleOptions = {
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: '123456',
      database: 'quiz',
      entities: [__dirname + '/../**/*.entity{.ts,.js}'],
      synchronize: true,
    };

dto

import { isNotEmpty, IsNotEmpty, Length } from 'class-validator';

export class CreateQuizDto {
  @IsNotEmpty({ message: 'Title mandatory' })
  @Length(3, 255)
  title: string;

  @IsNotEmpty()
  @Length(3)
  description: string;
}

实体

import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity('quizes')
export class Quiz extends BaseEntity {
  @PrimaryGeneratedColumn({
    comment: 'The quiz unique identifier',
  })
  id: number;

  @Column({
    type: 'varchar',
  })
  title: string;

  @Column({
    type: 'text',
  })
  description: string;

  @Column({
    type: 'varchar',
  })
  usernmae: string;

  @Column({
    type: 'date',
  })
  createddate: Date;

  @Column({
    type: 'boolean',
    default: 1,
  })
  isActive: boolean;
}

有人知道我该如何解决这个问题吗?

因为您正在使用 CustomRepository,您应该删除 InjectRepository 装饰器,然后让 NestJS 注入它。

quiz.service

constructor(
    private quizRepository: QuizRepository,
) {}

您还没有发布您的 typeorm.config 文件,因此请务必声明 Quiz 实体,以便 TypeORM 知道在哪里寻找它。

您使用的是哪个版本的 @nestjs/typeormtypeorm?如果您使用的 typeorm > 0.3,这是一个已知问题,使用所需的 typeorm 将解决该问题。

我有同样的问题,解决方案如下:你必须确保 nestjs/typeorm 的版本和 typeorm 在我的情况下是兼容的 我有 nestjs/typeorm 8.0.3 的版本和typeorm 0.3.6,这些版本不兼容,nest的8.0.3版本兼容0.2.34版本。当我将 typeorm 版本更改为 0.2.34 时,它编译成功。

package.json version image