nestjs: Injecting dependency into `useFactory` results in error.

Describe the bug From version 5.1.5 (PR) ImportingMikroOrm with forRootAsync and injecting any dependency in useFactory results in error.

Stack trace

TypeError: Cannot read properties of undefined (reading 'get')
    at Object.useFactory (/home/jdr/Workspace/xxx/src/api/core/core.module.ts:15:33)
    at Function.createEntityManager (/home/jdr/Workspace/xxx/node_modules/@mikro-orm/nestjs/mikro-orm-core.module.js:125:65)
    at Function.forRootAsync (/home/jdr/Workspace/xxx/node_modules/@mikro-orm/nestjs/mikro-orm-core.module.js:89:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

To Reproduce Steps to reproduce the behavior: Use MikroOrmModule.forRootAsync() and inject any dependency to useFactory.

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MikroOrmModule.forRootAsync({
      inject: [ConfigService],
      useFactory: async (configService) => {
        return {
          entities: [
            './dist/domain/**/model/*.js',
          ],
          dbName: configService.get('database.name'),
          type: 'postgresql',
          user: configService.get('database.user'),
          password: configService.get('database.password'),
          port: configService.get('database.port'),
          host: configService.get('database.host'),
          migrations: {
            path: __dirname + './dist/persistence/migrations',
            pattern: /^[\w-]+\d+\.js$/,
          },
        };
      },
    })
  ]
})

Expected behavior You should be able to use async provider without any errors thrown.

Versions

Dependency Version
node 16.13.2
typescript 4.9.4
mikro-orm 5.6.2
mikro-orm/nestjs 5.1.5

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I think you could probably create the entity manager as a provider so that the options can be injected into a factory for it. Would require a bit of re-work, but I think I might be able to help with that this week? I’ll have to clone the repo and see what can be done there