Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleRef error when registering command #18

Closed
timvandesteeg opened this issue Sep 26, 2018 · 1 comment
Closed

ModuleRef error when registering command #18

timvandesteeg opened this issue Sep 26, 2018 · 1 comment

Comments

@timvandesteeg
Copy link

timvandesteeg commented Sep 26, 2018

Getting the following error when I try to register my LoginCommandHandler. When I comment it out, it works just fine.

...\node_modules\@nestjs\core\injector\module-ref.js:24
            throw new unknown_element_exception_1.UnknownElementException();
                  ^
Error: Nest cannot find given element (it does not exist in current context)
    at Object.findInstanceByPrototypeOrToken (...\node_modules\@nestjs\core\injector\module-ref.js:24:19)
    at Object.get (...\node_modules\@nestjs\core\injector\module.js:259:29)
    at CommandBus.registerHandler (...\node_modules\@nestjs\cqrs\dist\command-bus.js:44:41)
    at handlers.forEach.handler (...\node_modules\@nestjs\cqrs\dist\command-bus.js:38:42)
    at Array.forEach (<anonymous>)
    at CommandBus.register (...\node_modules\@nestjs\cqrs\dist\command-bus.js:38:18)
    at AuthorizationModule.onModuleInit (...\src\modules\authorization\authorization.module.ts:63:25)
    at NestApplication.callModuleInitHook (...\node_modules\@nestjs\core\nest-application-context.js:65:39)

Minimal example:

@Module({
    imports: [
        CQRSModule
    ],
    providers: [
        LoginCommand,
        LoginCommandHandler <-- forgot this line
    ],
})
export class AuthorizationModule implements OnModuleInit {
    /**
     * @param {ModuleRef} _moduleRef
     * @param {CommandBus} _commands$
     */
    constructor(
        private readonly _moduleRef: ModuleRef,
        private readonly _commands$: CommandBus
    ) {}

    onModuleInit() {
        this._commands$.setModuleRef(this._moduleRef);

        this._commands$.register([
            LoginCommandHandler,
        ]);
    }
}
@timvandesteeg
Copy link
Author

Ugh, my bad.
Forgot to add CommandHandler to the providers array..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant