0

I have this:

const connector = mysql.createPool({
  connectionLimit: 20,
  host: "127.0.0.1",
  database: "todo",
  user: "root",
  password: "password",
});

executes :

const execute = async (query: string) => {
  try {
    const rows = await connector.query(query);
    console.log("done", rows);
    return rows;
  } catch (err) {
    // ERROR NEVER HERE --- WHY?
    console.log("DB ERROR => " + err);
    return err;
  }
};

export default execute;

calling:

execute("INSERT INTO tablename (column1) VALUES('hello')")

I had a typo when doing an INSERT and could not understand what was going wrong before I console logged the flow. I expected that query would throw the error to my catch but for some reason it never did, why?

CodingLittle
  • 1,761
  • 2
  • 17
  • 44
  • Which MySQL lib are you using? Guessing from the `createPool` call, it might be [this one](https://www.npmjs.com/package/mysql), which doesn't provide a promise. You'll want to wrap calls to it in promises if you want to use `await`, or use another lib that provides them itself. – T.J. Crowder Mar 06 '22 at 13:10
  • @T.J.Crowder mid illustrating with an answer that is adopted to the question? Cheers!. EDIT: Yes that is the correct lib – CodingLittle Mar 06 '22 at 13:13

0 Answers0