【问题标题】:How do I get an Entity's repository from the ApplicationContext如何从 ApplicationContext 获取实体的存储库
【发布时间】:2019-04-08 09:56:30
【问题描述】:

如何从 ApplicationContext 中获取实体的存储库

const ctx = await NestFactory.createApplicationContext(AppModule)
const repository = ctx.get<Repository<UserEntity>>(Repository);

// Error: Nest cannot find given element (it does not exist in current context)

【问题讨论】:

    标签: typescript nestjs


    【解决方案1】:

    想通了

    import ...
    import { UserEntity } from './user.entity';
    import { getRepositoryToken } from '@nestjs/typeorm';
    
    const ctx = await NestFactory.createApplicationContext(AppModule)
    const repository: Repository<UserEntity> = ctx.get(getRepositoryToken(UserEntity));
    

    【讨论】:

    • 您应该接受自己的答案,因为它可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多