typescript 嵌套JS/类型ORM:无法设置只有getter的#的属性元数据< Repository>

mmvthczy  于 2023-03-24  发布在  TypeScript
关注(0)|答案(4)|浏览(140)

I try to run my nestjstutorial app, the below error is showing. My backend is connected to a PostgreSQL db.
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)
My code looks like this:

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;
}

entity

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;
}

Somebody know how can I fix this?

41zrol4v

41zrol4v1#

我遇到了同样的问题,解决方案如下:你必须确保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

lf3rwulv

lf3rwulv2#

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

scyqe7ek

scyqe7ek3#

因为你使用的是CustomRepository,你应该删除InjectRepository装饰器,让NestJS注入它。

问答服务

constructor(
    private quizRepository: QuizRepository,
) {}

你还没有发布你的typeorm.config文件,所以一定要声明Quiz实体,这样TypeORM就知道在哪里查找它。

mwngjboj

mwngjboj4#

我在使用@nestjs/typeorm v8.1.4和TypeORM 0.3.12时也遇到了同样的问题
但是,由于v8.1.0,@nestjs/typeorm与TypeORM 0.3兼容。
因此,它应该在没有降级的情况下工作。但是,使用TypeORM 0.3做自定义仓库是完全不同的。
我能够通过遵循this article进行必要的更改。

相关问题