简体   繁体   中英

Whay can't I return an entity or an array of entities? -> (Nestjs Typeorm: cannot find module error)

I have a nestjs application, that I wrote in typescript and I use TypeOrm and MySQL Workbench database. I don't know what could go worng, as it can't find my entity. A got an error like:

Cannot find module 'C:/Users/HP/Documents/próba/test-crawler/src/database/entities/news.entity'

Although I only get this error when I want to retun an entity or an array of entities like:

return this.newsRepository.find()

Without this line the entity can be imported into the modules, everything is fine. I guess I did something wrong in the TypeOrm config, but I have already tried many ways I can't make it work.

Here is my repository:https://github.com/g937/web-crawler/tree/create-crawling-module

Write a file in your news module as a repository like news.repository.ts Add the code like below

import { EntityRepository, Repository } from "typeorm"
import { NewsEntity } from "../database/entities/news.entity";

@EntityRepository(NewsEntity)
export class NewsEntityRepository extends Repository<NewsEntity> {}

and in your controller replace

constructor(
    @InjectRepository(NewsEntity)
    private readonly newsRepository: Repository<NewsEntity>,
  ) {}

to

constructor(
    private readonly newsRepository: NewsEntityRepository,
  ) {}

Also change in the news.module.ts from

@Module({
  imports: [TypeOrmModule.forFeature([NewsEntityRepository])],
  providers: [NewsService],
  controllers: [NewsController],
})

to

@Module({
  imports: [TypeOrmModule.forFeature([NewsEntity])],
  providers: [NewsService],
  controllers: [NewsController],
})

Hopefully it will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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