Reputation: 43
After having followed the following tutorial in full: https://www.youtube.com/watch?v=JK5aynTxKjM&list=PL4bT56Uw3S4zIAOX1ddx8oJS6jaD43vZC
I decided to do my crud based on this: I tested TypeORM with my category (Categorie), so just a simple table with no foreign key or anything and it worked like a charm and the program runs CRUD. I said so I decided to make several tables including the "ProjectsLabo" table, with two foreign keys.
But problem when I incorporate in my module the three other modules I have an error: [Nest] 3100 - 25/01/2023, 14:39:29 ERROR [ExceptionHandler] Nest can't resolve dependencies of the ProjetslaboService (?). Please make sure that the argument ProjetslaboDtoRepository at index [0] is available in the ProjetslaboMo dule context.
Potential solutions:
@Module({
imports: [ /* the Module containing ProjetslaboDtoRepository */ ]
})
Error: Nest can't resolve dependencies of the ProjetslaboService (?). Please make sure that the argument ProjetslaboDtoRepository at index [0] is available in the ProjetslaboModule context.
Potential solutions:
@Module({
imports: [ /* the Module containing ProjetslaboDtoRepository */ ]
})
Here is an example of the LaboProjects module :
projetslabo.dto.ts
import { IsNotBlank } from "../../decorators/is-not-blank.decorator";
export class ProjetslaboDto {
//@IsString()
//@IsNotEmpty()
@IsNotBlank({message : 'Serveur : the name is not empty'})
nom?: string;
}
projetslabo.controller.ts
@Controller('projetslabo')
export class ProjetslaboController {
constructor(private readonly projetslaboService: ProjetslaboService) {
}
@Get()
async GetAll() {
return await this.projetslaboService.getAll();
}
@Get(':id')
async GetOne(@Param('id', ParseIntPipe) id: number) {
return await this.projetslaboService.findById(id);
}
}
projetslabo.entity.ts
@Entity({ name: 'projetslabo' })
export class ProjetslaboEntity {
@PrimaryGeneratedColumn()
idProjetsLabo: number;
@Column({ type: 'varchar', length: 40, nullable: false })
nom: string;
@ManyToOne(() => ValeurslaboEntity, (ValeurslaboEntity) => ValeurslaboEntity.idValeursLabo , {nullable : false})
FK_idValeursLabo: ValeurslaboEntity
@ManyToOne(() => AnneeslaboEntity, (AnneeslaboEntity) => AnneeslaboEntity.idAnneesLabo, {nullable : false})
FK_idAnneesLabo: AnneeslaboEntity
}
Projetslabo.module.ts
@Module({
imports: [TypeOrmModule.forFeature([ProjetslaboEntity])],
providers: [ProjetslaboService],
controllers: [ProjetslaboController],
})
export class ProjetslaboModule {}
projetslabo.repository.ts
@EntityRepository(ProjetslaboEntity)
export class ProjetslaboRepository extends Repository<ProjetslaboEntity>{}
projetslabo.service.ts
@Injectable()
export class ProjetslaboService {
constructor(
@InjectRepository(ProjetslaboDto)
private projetslaboRepository: ProjetslaboRepository,){}
async getAll(): Promise<ProjetslaboEntity[]> {
const list = await this.projetslaboRepository.find();
if (!list.length) {
throw new NotFoundException(
new MessageDto('Serveur : La liste est vide'),
);
}
return list;
}
async findById(idProjetsLabo: number): Promise<ProjetslaboEntity> {
const annees = await this.projetslaboRepository.findOneBy({
idProjetsLabo,
});
if (!annees) {
throw new NotFoundException(
new MessageDto("Serveur : Cette année n'existe pas"),
);
}
return annees;
}
app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
envFilePath: '.env',
isGlobal: true,
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mariadb',
host: configService.get<string>(DB_HOST),
port: +configService.get<number>(DB_PORT),
username: configService.get<string>(DB_USER),
password: configService.get<string>(DB_PASSWORD),
database: configService.get<string>(DB_DATABASE),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
logging: true,
}),
inject: [ConfigService],
}),
CategoriesModule,
/*Problem with this 3 modules
ProjetslaboModule,
ValeurslaboModule,
AnneeslaboModule,
*/
],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}
Can you help me please? Nestjs/core : 9.0.0 Nestjs/typeorm : 9.0.1
Upvotes: 0
Views: 205
Reputation: 70450
You pass ProjetslaboDto
to your @InjectRepository()
when you should be passing ProjetslaboEntity
as that's what you've told TypeOrmModule.forFeature()
about.
@Injectable()
export class ProjetslaboService {
constructor(
- @InjectRepository(ProjetslaboDto)
+ @InjectRepository(ProjetslaboEntity)
private projetslaboRepository: ProjetslaboRepository,){}
...
Upvotes: 1