Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
No detectar un error de async await a pesar de usar bloques de captura de prueba

Estoy usando un bucle para obtener todos los mensajes de WhatsApp asociados con números dados, usando whatsappweb-js. Cuando hay un error en esta línea "let chat = await client.getChatById(nums[i])" no puedo detectarlo, en su lugar aparece un error en la siguiente línea que indica que "chat is not added" (el chat no está definido). si elimino try catch, no obtengo ningún error, pero la aplicación node js simplemente deja de responder nota: client.getChatById (id) devuelve una promesa que contiene el objeto de chat.

 function fetch(nums) { for (let i = 0; i < nums.length; i++) { try { let chat = await client.getChatById(nums[i]) } catch (err) { console.log(err) }; } return msgs = await chat.fetchMessages(); }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El código que usa el chat debe estar dentro del bloque de try . Esto es necesario por razones de alcance y también porque no tiene sentido usar el chat si la llamada falla.

Además, no debería tener return dentro del ciclo, ya que desea recopilar los resultados de todos los chats. Declare una matriz para contenerlos, agregue a la matriz en el ciclo y luego regrese al final.

 async function fetch(nums) { let msgs = []; for (let i = 0; i < nums.length; i++) { try { let chat = await client.getChatById(nums[i]); msgs = msgs.concat(await chat.fetchMessages()); } catch (err) { console.log(`while fetching ${nums[i]}`, err) }; } return msgs; }

about 4 years ago · Juan Pablo Isaza Report

0

Necesitas mover la devolución:

 function fetch(nums) { for (let i = 0; i < nums.length; i++) { try { let chat = await client.getChatById(nums[i]) return msgs = await chat.fetchMessages(); } catch (err) { console.log(err) }; }

Está poniendo su variable de chat fuera de try/catch, por lo que no sabría cuál es la variable.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!