AdonisJS 6 Event Emitter Type Error and passed data undefined #4740
-
I was following the docs to create an event on Emitter Docs.
and an Listener
then I first tried in start/events.ts, following the docs, to use the
but I'm constantly getting a type error in my ide
while ts.config.json is the one preconfigured from adonisjs
Then I tried to use the
This works now without any type error but the user object (model) is undefined in the SendWelcomeEmail Listener. I dispatch the event in the controller like that
I'm struggeling to get this simple thing to work and I tried all I can imagine. Anyone have an idea why both examples don't work? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In your listener, you get the event and not the User. So it should be import User from "#models/user";
+ import UserRegistered from '#events/user_registered'
import RegisteredUserEmailService from "#services/emails/app/registered_user_email_service";
import { inject } from "@adonisjs/core";
@inject()
export default class SendWelcomeEmail {
constructor(protected emailService: RegisteredUserEmailService) { }
- handle(user: User) {
- this.emailService.welcomeEmail(user);
+ handle(event: UserRegistered) {
+ this.emailService.welcomeEmail(event.user);
}
} |
Beta Was this translation helpful? Give feedback.
In your listener, you get the event and not the User. So it should be