简体   繁体   English

为什么我不能返回一个实体或一组实体? ->(Nestjs Typeorm:找不到模块错误)

[英]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.我有一个 nestjs 应用程序,我在 typescript 中编写,我使用 TypeOrm 和 MySQL Workbench 数据库。 I don't know what could go worng, as it can't find my entity.我不知道 go 会磨损什么,因为它找不到我的实体。 A got an error like: A 出现如下错误:

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.我想我在 TypeOrm 配置中做错了什么,但我已经尝试了很多方法,但我无法让它工作。

Here is my repository:https://github.com/g937/web-crawler/tree/create-crawling-module这是我的存储库: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在您的新闻模块中编写一个文件作为存储库,如news.repository.ts添加如下代码

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并在您的 controller 替换

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

to

constructor(
    private readonly newsRepository: NewsEntityRepository,
  ) {}

Also change in the news.module.ts from也改变news.module.ts

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

to

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

Hopefully it will work希望它会奏效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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