NestJS EntityMetadataNotFoundError:找不到“存储库”的元数据

2023-12-26

我知道有很多关于这个主题的帖子。我真的很难理解我到底想做什么来解决这个问题。使用 Postman,当我尝试命中路线时,出现以下错误:

ERROR [ExceptionsHandler] No metadata for "OrganizationsRepository" was found.
EntityMetadataNotFoundError: No metadata for "OrganizationsRepository" was found.

这是我的代码的样子

// app.module.ts

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      database: 'my-database',
      username: 'postgres',
      password: 'password',
      autoLoadEntities: true,
      synchronize: true,
    }),
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    OrganizationsModule,
  ],
  controllers: [],
  providers: [],
  exports: [],
})
export class AppModule {}
// organizations.repository.ts

@EntityRepository(Organization). // this is showing as deprecated
export class OrganizationsRepository extends Repository<Organization> {
...
}
// organization.entity.ts

@Entity({ name: 'organizations' })
export class Organization extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;
...

我相信我需要创建一个新的DataSource——或者更具体地说,自定义存储库?

我一直使用上面的方法从我的数据库中读取数据,没有任何问题。现在突然间我收到此错误,并且我不确定如何在我的代码中解决。


EntityRepository 已被弃用,因此您可以用 Injectable() 替换它,然后您需要修改您的存储库、模块和服务。我给你举了例子,希望对你有帮助。

存储库.ts

import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Organization } from './Organization.entity';
 
@Injectable() // here
export class OrganizationRepository extends Repository<Organization> {
  constructor(private dataSource: DataSource) {
    super(Organization, dataSource.createEntityManager());
  }

        sync createOrganization({ title, description }: CreateOrganizationDto):Promise<Organization> {...}
  }

组织.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Organization } from './Organization.entity';
import { OrganizationController } from './Organization.controller';
import { OrganizationRepository } from './Organization.repository';
import { OrganizationService } from './Organization.service';
 
@Module({
  imports: [TypeOrmModule.forFeature([Organization])],
  controllers: [OrganizationController],
  providers: [OrganizationService, OrganizationRepository],
})
export class OrganizationModule {}

组织.service.ts

import { Injectable, NotFoundException } from '@nestjs/common';
import { OrganizationRepository } from './organization.repository';
import { InjectRepository } from '@nestjs/typeorm';
import { Organization } from './organization.entity';
 
@Injectable()
export class OrganizationService {
  constructor(private readonly OrganizationRepository: OrganizationRepository) {}
 
  async getOrganizationById(id: string): Promise<Organization> {
    const record = this.OrganizationRepository.findOne({ where: { id } });
    if (!record) {
      throw new NotFoundException();
    }
    return record;
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NestJS EntityMetadataNotFoundError:找不到“存储库”的元数据 的相关文章

随机推荐

  • 插件“FEDERATED”已禁用

    我尝试使用 easyPHP 启动 MySQL 响应是日志文件的警报窗口 主要错误是 通过网上研究 我发现解决方案是将联合选项添加到my ini文件 我这样做了 但它仍然不起作用 以下是日志文件的摘录 以了解更多信息 2013 05 03 1
  • 获取点击元素的ID

    div div div div 尝试此方法来获取单击的元素的 ID 并发出警报 我确信这是我所缺少的一些非常基本的东西 有人可以帮忙吗 这实际上非常基本 停止使用内联事件处理程序
  • 每次尝试以特定顺序循环 3 个线程

    我的问题是如何让一个线程运行 然后再运行一次 然后再次运行 然后它会重复本身 我有一个主文件 private static ThreadManager threadManager public static void main String
  • 通用选择不适用于位类型

    基于这个答案 https stackoverflow com a 18469483 1993545 我尝试为我的桌子创建一个选择 ALTER PROCEDURE Einrichtung Select Parameters with defa
  • 如何在 Jetpack Compose 中向图标添加阴影/边框/高度

    我想在 Jetpack compose 中为我的图标添加阴影 以便图像和文本具有 大致 相似的阴影 Text text HAS SHADOW style MaterialTheme typography body2 copy shadow
  • 打印汉字的ESC/POS命令

    打印机型号 爱普生TM T88V ESC POS命令指南 见P 115 http download delfi com SupportDL Epson Manuals TM T88IV Programming 20manual 20APG
  • 是否需要将原始类型键入枚举?

    我正在浏览NSString查看头文件 看看 Apple 如何编写枚举 并发现了这段代码 enum NSStringEncodingConversionAllowLossy 1 NSStringEncodingConversionExtern
  • Google DataFlow/Python:save_main_session 和 __main__ 中的自定义模块导入错误

    有人可以澄清使用时的预期行为吗save main session和导入的自定义模块 main 我的 DataFlow 管道导入 2 个非标准模块 一个通过requirements txt另一个通过setup file 除非我将导入移至使用它
  • 从txt中解析IP地址

    我正在尝试下载一个txt您可以找到的文件here http proxy ip list com download free proxy list txt 下载文件不是问题 testfile urllib URLopener testfile
  • 创建数组的副本并操作原始数组

    首先我要为我糟糕的英语道歉 我会尽力说得清楚 我有一个 3 维数组 只是一个 2 维数组的数组 我的目标是获取其中一个二维数组 并将其逆时针旋转 90 它看起来像这样 1 2 3 4 5 6 7 8 9 我尝试让它像这样 旋转 3 6 9
  • 发生类型错误:超出翻译容量

    当我运行我的示例时 出现以下类型错误 Translation capacity exceeded In this scope universe contains 21 atoms and relations of arity 8 canno
  • 未捕获的 RangeError:超出了最大调用堆栈大小,JavaScript

    我有个问题 open function type Some code document getElementById type addEventListener click l close type false close function
  • 写入开始后无法设置此属性!在 C# WebRequest 对象上

    我想重用 WebRequest 对象 以便保存 cookie 和会话以供以后向服务器请求 下面是我的代码 如果我第二次使用 Post 函数两次 request ContentLength byteArray Length 它会抛出异常 写入
  • 分叉的 IORef 读取器函数似乎会停止主线程

    我正在对并发性和内存可见性进行一些实验 并遇到了这种奇怪的行为 请参阅内联评论 module Main where import Data IORef import Control Concurrent import System CPUT
  • 在 PyQt 中打开第二个窗口

    我正在尝试使用 pyqt 在单击 QMainWindow 上的按钮时显示自定义 QDialog 窗口 我不断收到以下错误 python main py DEBUG Launch edit window Traceback most rece
  • 在 R 中使用 phantomJS 抓取具有动态加载内容的页面

    背景我目前正在使用 rvest 从 R 的一些网站上抓取产品信息 这适用于除一个网站之外的所有网站 其中内容似乎是通过 angularJS 动态加载的 因此无法迭代加载 例如通过 URL 参数 就像我对其他网站所做的那样 具体网址如下 ht
  • Zend Framework - 如何将 url 重写为 seo 友好的 url

    我得到了 Zend Framework 的网站 我在 Zend 中完全是菜鸟 例如我想制作一个网址 somewebsite com test about 看起来像这样 somewebsite com for fun link 我如何在 Ze
  • 无法找到“Firebase”的规范

    我在尝试时无法安装 Firebase pod pod install Analyzing dependencies Unable to find a specification for Firebase 这是我的 Podfile platf
  • 导入 javax.servlet 仍然无法解析[重复]

    这个问题在这里已经有答案了 这是我的 Eclipse 范围的 JRE 定义 清楚地显示已添加 servlet api jar 这里有同样的旧导入错误 似乎永远无法解决 Java不是很棒吗 有人有可能对为什么这仍然不起作用有一个简单 事实的答
  • NestJS EntityMetadataNotFoundError:找不到“存储库”的元数据

    我知道有很多关于这个主题的帖子 我真的很难理解我到底想做什么来解决这个问题 使用 Postman 当我尝试命中路线时 出现以下错误 ERROR ExceptionsHandler No metadata for OrganizationsR