#service in main.ts

22 messages · Page 1 of 1 (latest)

celest thicket
#

i do not know if this is deserves its own post
this could be a bug (i'm not sure)
if i wanted to use a service in the main.ts that i would just have to do
const service = app.get(MyService) for example loading a custom config service

but it seems this no longer works according to wodCZ { strict: false } would need to be added at first this seemed to work untill it didn't
i managed to get this working by using the service as a provider in the app.module
i will tag this as solved because i got it working
but is this indented/right/a bug/else? at this point i'm just confused

wide comet
#

but it works

#

even if we have this

celest thicket
# wide comet but it works

by adding the service as a provider for the app.module yeah
but my older and structurally messed up nest project

was just a matter of const config = app.get(ApiConfigService) in the main.ts and it worked

craggy relic
# wide comet even if we have this

Why does this work? Not only it's not directly in the AppModule (I thought I have to select to the module if I didn't want to use strict). But also, it's not exported 🤔

wide comet
#

I guess the default for that .get is strict: false

#

yep, that's why

craggy relic
#

It is 🤯 I swear it didn't work without { strict: false } before. Or do I just need a vacation? 🤔

celest thicket
#

that's why i'm so confused i now need to use a provider for it to work

#

otherwise it doesn't understand the context

celest thicket
craggy relic
#

Isn't it request scoped (even by accident)?

celest thicket
# craggy relic Isn't it request scoped (even by accident)?

sorry dont know what you're trying to say here
everything in the app.module is disabled
and in the main.ts disabled anything but this

const logger = new Logger('Bootstrap')

  // create nest application
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    logger: ['error', 'warn', 'debug', 'verbose', 'log'],
  })
  //app.setGlobalPrefix('/api')

  // access the configService
  const config = app.get(ApiConfigService, { strict: false })
  await app.listen(3000)
craggy relic
#

Whether ApiConfigService depends on request, or other service that depends on request

celest thicket
craggy relic
#

You checked the usuals ApiConfigService service has @Injectable, is in providers and exports of a module, and that module is in imports array within AppModule or it's imports, right?

celest thicket
#
@Injectable()
export class ApiConfigService {
  private readonly logger = new Logger(ApiConfigService.name)

  constructor(private config: ConfigService) {}
  ...
}
@Global()
@Module({
  imports: [ConfigModule],
  providers: [ApiConfigService],
  exports: [ApiConfigService],
})
export class ApiConfigModule {}
craggy relic
#

ApiConfigModule is imported in AppModule?

celest thicket