Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
i want to use nestjs emitter from main.ts. i have function in controller class which has @OnEvent('send>email'). please guid me how can i invoke event
  1. Hi, i am new in nodejs please guid me. this is main.ts class which have statement to invoke OnEvent('send_email') in controller class you can see controller class also.
  2. i added it
       async function bootstrap() {
      const myEmitter = new EventEmitter2();
      const app = await NestFactory.createMicroservice(
        AppModule,
        appConfig.microservice,
      );
    
      //#region RabbitMQ Implementation
      const email = {
        email: {
          from: 'GeoNoon <xzy@yahoo.com>',
          to: 'ali.whu@yahoo.com',
          subject: 'Hello ✔',
          html: '<b>Hello world? 测试邮件</b>',
        },
      };
      amqplib.connect(
        'amqp://localhost',
        (error0, connection) => {
          if (error0) {
            throw error0;
          }
          connection.createChannel((error1, channel) => {
            if (error1) {
              throw error1;
            }
            const queue = 'Test_emails_queue';
            channel.assertQueue(queue, { durable: false });
            channel.sendToQueue(queue, Buffer.from(JSON.stringify(email)));
            console.log(' [*] Waiting for message', queue);
            // myEmitter.on('send_email', function(email:{}) {
            //   console.log(this.event, email);
            // });
            const response = myEmitter.emit('send_email', JSON.stringify(email));
            console.log(response);
          });
        },
      );
      //#endregion
    
      await app.listen();
      console.log('Geonoon Mail Microservice is listening');
    }
    bootstrap();
  1. this is app.controller class which has method to send email to customer.
  2. please help me how can i do this. thanks
       export class AppController {
  constructor(private readonly appService: AppService) {}

  @OnEvent('send_email')
  async handlerEmailSend(data: Record<string, any>) {
    console.log(data);
    if (
      data.email &&
      data.email.from &&
      data.email.to &&
      data.email.subject &&
      (data.email.html || data.email.text)
    ) {
      return await this.appService.sendEmail(data as EmailContent);
    } else {
      Logger.warn('电子邮件数据格式错误');
    }
  }
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda