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

109
Views
La función asíncrona responde antes que los datos dentro de la función

Estoy tratando de trabajar con una base de datos en node.js. Tengo una función asíncrona para verificar y ver si existe un registro en la base de datos. Entonces la función debería devolver verdadero o falso.

Actualmente, la función regresa undefined y en los registros veo que las líneas de console.log que están en esa función se muestran después de lo que debería ser el valor devuelto por la función asíncrona.

Aquí está mi código

 const PostgresCheckExist = async (id) => { try { console.log(`-=STARTING POSTGRES CHECK EXISTS=-`); client.query(`SELECT token from oauth where id = '${id}';`, (err, res) => { console.log(res.rowCount); const rowCount = res.rowCount; if (err) throw err; if (rowCount <= 0) { console.log(`The row count was ${rowCount}!\nEntered the <= if marker`); client.end(); console.log(`Made is passed the 'client.end()' call!`); return false; } else if (rowCount >= 2) { client.end(); console.log(`The row count was ${rowCount}!\nEntered the >= if marker`); throw "ERROR: More than one item returned on for Primary Key. Please check database"; } else { console.log(`The row count was ${rowCount}!\nEntered the else marker`); console.log(rowCount); client.end(); return true; } }); } catch (err) { console.error(err); } }; v1.get("", async (req, res) => { const respone = await PostgresCheckExist(1) console.log(response); res.json({ success: false }); });

Esto se llama desde un enrutador express.

También aquí está la salida del comando.

 app[web.1]: -=STARTING POSTGRES CHECK EXISTS=- app[web.1]: undefined app[web.1]: 0 app[web.1]: The row count was 0! app[web.1]: Entered the <= if marker app[web.1]: Made is passed the 'client.end()' call!
about 4 years ago · Juan Pablo Isaza
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!