Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda