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

135
Views
Pasar el argumento a la función async/await devuelve "indefinido"

Al publicar datos en una API y obtener una respuesta, si codifico los datos del cuerpo dentro de la llamada de búsqueda (cuerpo: "XYZ12345") funciona bien, este es un ejemplo:

 const vatValidationRequest = fetch( '/api/vies/validateVAT.php', { method: 'POST', body: "XYZ12345", headers: { 'Content-Type': 'application/text' } }) .then((response) => response.text()) .then((responseText) => { return responseText; }); const validateVAT = async () => { const viesResponse = await vatValidationRequest; console.log(viesResponse); }; validateVAT();

Sin embargo, si trato de pasar los datos del cuerpo como un argumento (cuerpo: vatNumber), la función validateVAT() devuelve "indefinido". Esto es lo que no funciona:

 const vatValidationRequest = (vatNumber) => { fetch( '/api/vies/validateVAT.php', { method: 'POST', body: vatNumber, headers: { 'Content-Type': 'application/text' } }) .then((response) => response.text()) .then((responseText) => { return responseText; }); } const validateVAT = async (vatNumber) => { const viesResponse = await vatValidationRequest(vatNumber); console.log(viesResponse); }; validateVAT("XYZ12345");

¿Alguna pista sobre cómo pasar el argumento a la función asíncrona? ¡Gracias!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El problema es que no está devolviendo la respuesta del método. Usted debe hacer esto:

 const vatValidationRequest = (vatNumber) => { return fetch( '/api/vies/validateVAT.php', { method: 'POST', body: vatNumber, headers: { 'Content-Type': 'application/text' } }) .then((response) => response.text()) .then((responseText) => { return responseText; }); } const validateVAT = async (vatNumber) => { const viesResponse = await vatValidationRequest(vatNumber); console.log(viesResponse); }; validateVAT("XYZ12345");
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!