0

I try to store the result of my mysql request into and async function (to make something after storing my result) but it's returning undefined.. I don't know why

function hh () {
  connection.query('SELECT * FROM `rounds` ', function (error, results, fields) {
    if (error) throw error;
    // console.log(results)
    return results
  });
}

async function run() {
  connection.connect();
  let deter = await hh();
  console.log(deter)
  connection.end();
}

run();

1 Answer 1

1

You're not returning a promise... Try with this code, it could help...

function hh () {
  return new Promise((resolve, reject) => {
      connection.query('SELECT * FROM `rounds` ', function (error, results, fields)   {
          if (error) return reject(error);
          // console.log(results)
          resolve(results)
      });
  });
}

async function run() {
  connection.connect();
  let deter = await hh();
  console.log(deter)
  connection.end();
}

run();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.