NestJS新建组件依赖注入报错,运行时出现502网关错误
NestJS 502 Bad Gateway 及依赖注入错误排查
项目运行时抛出502 Bad Gateway错误,注释app.module.ts中相关模块后代码可正常运行。该模块仅通过数据仓库连接数据库,核心代码如下:
模块导入部分
imports : [ TypeOrmModule.forFeature([KYCStatus, User, Country], 'customerDB'), ]
服务构造函数
@InjectDataSource('customerDB') private readonly dataSource: DataSource,
使用DataSource获取仓库
const countryRepository = this.dataSource.getRepository(Country);
抛出的错误信息
Error: Nest can't resolve dependencies of the customerDB_KYCStatusRepository (?). Please make sure that the argument customerDBDataSource at index [0] is available in the TypeOrmModule context.
排查方向
- 确认
TypeOrmModule是NestJS的有效模块 - 检查
customerDBDataSource这个提供者是否属于当前TypeOrmModule上下文 - 如果
customerDBDataSource是从独立模块导出的,需确认该模块已在TypeOrmModule中导入,示例结构:
@Module({ imports: [ /* 包含customerDBDataSource的模块 */ ] })
内容的提问来源于stack exchange,提问作者Rohit Mittal
相关产品推荐
相关产品推荐

