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

193
Visualizações
How to send qr code string response in whatsapp-web.js?

I'm trying to send the qr code of whatsapp-web.js to my frontend with the code bellow

const router = require("express").Router();

router.get("/", async (req: any, res: any) => {
  const qrcode = require("qrcode-terminal");

  const { Client, LocalAuth } = require("whatsapp-web.js");

  const client = new Client({
    authStrategy: new LocalAuth(),
  });
  client.on("qr", (qr: any) => {
    // qrcode.generate(qr, { small: true });

    res.send(qr)
  });

  client.on("ready", (qr:any) => {
    console.log("Client is ready!");

    const number = [
      { phone: "number1", name: "name1" },
      { phone: "number2", name: "name2" },
    ];

    number.forEach((el) => {

      // Your message.
      const text = `teste ${el.name}`;

      // Getting chatId from the number.
      // we have to delete "+" from the beginning and add "@c.us" at the end of the number.
      const chatId = el.phone.substring(1) + "@c.us";

      // Sending message.
      client.sendMessage(chatId, text);
    });
  });

  client.initialize();

  res.status(200).json("ok");
});

module.exports = router;

but it's not sending. Maybe because there's a delay to generate this code. Can you guys help me

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're registering a callback that sends the qr when the "qr" event is emitted. The res.status(200) is executed before the event is emitted (Even if it is emitted immediately the callback will be executed after the rest of the function since Node is single threaded).

I'm not familiar with the whatsapp package but if you know the qr event will only be emitted once or you only need the first emission you can wrap it in a promise.

Try

router.get('/', async (req, res) => {
    const client = new Client(...)
    let qr = await new Promise((resolve, reject) => {
        client.once('qr', (qr) => resolve(qr))
    })
    res.send(qr)
}

Notice, I used client.once so the callback is unregistered from the client's event after one event is emitted to prevent memory leaks.

It is a good practice to add a reject scenario to prevent cases the promise doesn't resolve

router.get('/', async (req, res) => {
    try {
        const client = new Client(...)
        let qr = await new Promise((resolve, reject) => {
            client.once('qr', (qr) => resolve(qr))
            setTimeout(() => {
                reject(new Error("QR event wasn't emitted in 15 seconds."))
            }, 15000)
        })
        res.send(qr)
    } catch (err) {
        res.send(err.message)
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
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