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

118
Views
Async Function is responding before the data inside the function

I am trying to work with a database in node.js I have an async function to check and see if a record exists in the database. Then the function should return true or false.

Currently the function is returning undefined and in the logs I see that the console.log lines that are in that function are being displayed after what should be the returned value from the async function.

Here is my code

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 });
});

This is being called from an express router.

Also here is the output from the command

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!