#this provider does not exist in the current context

7 messages · Page 1 of 1 (latest)

rustic wave
#

Hi All, I have environment variable set and when I try to infer in my project, says
Error - this provider does not exist in the current context , Am I doing something wrong
Would we not be able to infer in app.get() the constants ?
customService and customServiceName are the same

main.ts file

import { customService } from './module/customServiceName.service'

const customServiceName = process.env.CUSTOM_SERVICE
if (customServiceName) {
const service = app.get(customServiceName)
} else {
// do something
}

mystic scroll
#

Do you register a provider via provide: process.env.CUSTOM_SERVICE in a custom provider?

rustic wave
#

hi, thank you for your response - customService already has a @dapper bloomable() decorator on it, do i have to perform anything on main.ts file, when i try to infer the actual servicename in the main.ts file, it works, only when I try to infer through the variable, it does not work

#

In customServiceName.service.ts file

@dapper bloomable()
export class customService {

}

#

const service = app.get(customService) -- works

mystic scroll
#

Nest uses references to the class for the injection token by default, not just the name of the class, so you can't app.get(ClassName) when you really need to app.get(ClassReference). You'd need to sett up custom providers like

{
  provide: 'ClassName',
  useClass: ClassReference,
}

Or if you already have ClassReference in the providers then something like

{
  provide: 'ClassName',
  useExisting: ClassReference
}

Would work too

rustic wave
#

checking.. thank you again