nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

APP_GUARD #843

Closed dyaacov closed 6 years ago

dyaacov commented 6 years ago

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)

`

adrien2p commented 6 years ago

The AuthModule export the AuthService ?

dyaacov commented 6 years ago

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)

`

cojack commented 6 years ago

@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.

dyaacov commented 6 years ago

resolved!

see https://github.com/nestjs/nest/issues/841

lock[bot] commented 4 years ago

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.