I'm trying to inject user repository to UserService, but it seems that typeorm connection is not making. I've googled about this error, but it seems there's not much resource.
Here's an error log
[Nest] 18226 - 2021. 11. 30. 오후 7:10:19 ERROR [ExceptionHandler]
Nest can't resolve dependencies of the UserRepository (?).
Please make sure that the argument Connection at index [0] is available
in the TypeOrmModule context.
Potential solutions:
- If Connection is a provider, is it part of the current TypeOrmModule?
- If Connection is exported from a separate @Module, is that module imported within TypeOrmModule?
@Module({
imports: [ /* the Module containing Connection */ ]
})
Here's codes
user.service.ts
@Injectable()
export class UserService {
constructor(
@InjectRepository(User) private readonly user: Repository<User>,
) {}
...
}
user.module.ts
@Module({
imports: [TypeOrmModule.forFeature([User])],
providers: [UserService],
exports: [UserService],
controllers: [UserController],
})
export class UserModule {}
app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
...
}),
TypeOrmModule.forRoot({
type: 'postgres',
...,
synchronize: process.env.NODE_ENV !== 'prod',
logging:
process.env.NODE_ENV !== 'prod' && process.env.NODE_ENV !== 'test',
entities: [User, Wiki],
}),
MongooseModule.forRoot(process.env.MONGODB_ROOT),
UserModule,
WikiModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
For someone who's struggling with this issue, just try TypeOrmModule.forRootAsync()
TypeOrmModule.forRootAsync({
useFactory: () => ({
type: 'postgres',
...,
synchronize: process.env.NODE_ENV !== 'prod',
logging:
process.env.NODE_ENV !== 'prod' && process.env.NODE_ENV !== 'test',
}),
})