#Issue with DI

58 messages · Page 1 of 1 (latest)

obtuse forge
#

I have a AuthGuard which is importing several services like shown.

constructor(
    private readonly jwtService: JwtService,
    private readonly reflector: Reflector,
    private readonly errorsService: ErrorsService,
    private readonly userService: UserService,
  ) {}

Everything here works as expected when I am not injecting userService. When I try to inject UserService none of them gets injected and logging them shows undefined. The UserModule is Global and is imported into AppModule. Incase constructor order matters, the UserModule is imported after all the other services mentioned here. Not sure what is wrong since I assumed that a Global module's exported Serivce should be available everywhere for import.

The Auth Guard is not a part of any Module. It is directly imported into AppModule's providers

left vault
#

My money is on circular imports (on the file level)

nova urchin
#

What is meant by "not working" in this case?

obtuse forge
#

none of the services are available in the activate method

nova urchin
#

None get injected: okay, is the UserService REQUEST scoped?

obtuse forge
#

Nope

#

There is another provider that userService uses which is request scoped though

#

But I am making a request????

nova urchin
#

Then it is request scope

#

Yeah, but global Enhancers do weird things with request scoped providers

#

Better to inject the module reg and resolve the user service dynamically

obtuse forge
#

Also there is no circular import. Userservice does not import auth guard.

obtuse forge
nova urchin
obtuse forge
#

Ah ok

#

wdym by resolve the user service?

#

I can just forget about using the user service and inject the connection here and get the repository of the user model FeelsBadMan

nova urchin
#
const ctxId = ContextIdFactory.getIdByRequest(context.switchToHttp().getRequest());
const userService = await this.moduleRef.resolve(UserService, { strict: false }, ctxId)
obtuse forge
#

But the point is that this is not something that isnt supposed to work from what I have understoof so far

#

is there a debugger I can run?

nova urchin
obtuse forge
nova urchin
obtuse forge
#

let me try that. if I find something I will write back here

obtuse forge
nova urchin
obtuse forge
#

like put authguard as a provider in authmodule

#

can it be that when things are at the authguard the request scoped resources have not been created yet?

nova urchin
nova urchin
obtuse forge
#

Ik you did but that isnt a clean way imo. just doesnt sit right with me. I will look around a bit. If nothing then will have to use that ig

nova urchin
#

What is it with people not taking my advice today and thinking I'm holding out on some sort of secret "elite" way to do things?

obtuse forge
#

Sorry didnt mean it that way

#

I appreciate your help lol.

#

And appreciate you looking into this in the future

nova urchin
#

This is the second or third time I've said to someone "you can't easily, do this instead" and they insist on looking for something else. Weird that it keeps happening

obtuse forge
#

Its just that when something is supposed to work and it doesnt I dont get peace of mind.

obtuse forge
obtuse forge
#

Lmao ig I really will have to work with the manual way of resolving this

#

sighers

obtuse forge
#
[Nest] 488706  - 11/07/2023, 6:32:22 AM   ERROR [ExceptionsHandler] Nest could not find UserService element (this provider does not exist in the current context)

@nova urchin ^^

nova urchin
#

Does the route that you're hitting inject the UserService?

#

Don't forget the { strict: false }

obtuse forge
#

The route I am hitting is the UserModule route xD

#

strict false throws type error

#

getIdByRequest is also not a function

nova urchin
#

Yeah it's getByRequest I was recalling the method and signature by memory on mobile

#

It should get getByRequest(UserService, ctxId, { strict: false })

obtuse forge
#

ah

#

that worked.

#

lemme try a req

#

damn. finally. thanks a lot. I can finally sleep.

#

guess I aint working today lmao.