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:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialsrcentity-managerEntityManager.ts:1404:59)
at DataSource.getCustomRepository (D:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialsrcdata-sourceDataSource.ts:465:29)
at InstanceWrapper.useFactory [as metatype] (D:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialnode_modules@nestjstypeormdisttypeorm.providers.js:13:35)
at Injector.instantiateClass (D:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialnode_modules@nestjscoreinjectorinjector.js:333:55)
at callback (D:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialnode_modules@nestjscoreinjectorinjector.js:48:41)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Injector.resolveConstructorParams (D:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialnode_modules@nestjscoreinjectorinjector.js:122:24)
at Injector.loadInstance (D:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialnode_modules@nestjscoreinjectorinjector.js:52:9)
at Injector.loadProvider (D:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialnode_modules@nestjscoreinjectorinjector.js:74:9)
at Injector.lookupComponentInImports (D:GaneshMyDrivenestjsnestjs_tutorialnestjsturorialnode_modules@nestjscoreinjectorinjector.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
}
Somebody know how can I fix this?