Nest: APP_GUARD

Created on 4 Jul 2018  路  5Comments  路  Source: nestjs/nest

Hi All,
I'm using a global AuthGuard, I need to inject my auth-service to the AuthGuard constructor

`@Module({
imports: [AuthModule, UsersModule],
providers: [
{
provide: APP_GUARD,
useClass: AuthGuard,
},
],
})

@Injectable()
export class AuthGuard implements CanActivate {
constructor(private readonly authService: AuthService) { }

I get

Error: Nest can't resolve dependencies of the APP_GUARD (?). Please verify whether [0] argument is available in the current context.
at Injector.lookupComponentInExports (/Users/dekely/dev/NestJS-Starter/node_modules/@nestjs/core/injector/injector.js:129:19)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
at startup (internal/bootstrap/node.js:238:19)

`

question 馃檶

Most helpful comment

@dyaacov show your repo

can you explain why of course, because if you would like to use a service outside from a module, you just have to export it, just it. Treat module it like a black box, if you don't make any whole for in, you won't be able to take anything from inside. This is what exports are for.

All 5 comments

The AuthModule export the AuthService ?

No, should he? can you explain why

when I export it I get different error:
`
(node:32503) UnhandledPromiseRejectionWarning: Error: Nest cannot find given element (it does not exist in current context)
at NestApplicationContext.findInstanceByPrototypeOrToken (/Users/dekely/dev/NestJS-Starter/node_modules/@nestjs/core/nest-application-context.js:70:19)
at NestApplicationContext.find (/Users/dekely/dev/NestJS-Starter/node_modules/@nestjs/core/nest-application-context.js:38:21)
at NestApplicationContext.get (/Users/dekely/dev/NestJS-Starter/node_modules/@nestjs/core/nest-application-context.js:32:25)
at eval (webpack:///./src/server.ts?:19:70)
at Generator.next ()
at fulfilled (webpack:///./src/server.ts?:4:58)

`

@dyaacov show your repo

can you explain why of course, because if you would like to use a service outside from a module, you just have to export it, just it. Treat module it like a black box, if you don't make any whole for in, you won't be able to take anything from inside. This is what exports are for.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chaostheory picture chaostheory  路  34Comments

kamilmysliwiec picture kamilmysliwiec  路  178Comments

BrunnerLivio picture BrunnerLivio  路  40Comments

roeehershko picture roeehershko  路  27Comments

j picture j  路  32Comments