I've a problem, I want to create a module that exports various call to db and use it with await in my app... here an example:
// module
const config = require("../config/config");
const mysql = require("mysql2/promise");
const getUsersData = async (args) => {
const db = await connectDb();
let rows, fields;
try {
[ rows, fields ] = await db.query("SELECT * FROM users WHERE id !== ? LIMIT ?", [0, 10]);
} catch (e) {
return e;
}
return rows;
};
const connectDb = async () => {
const db = await mysql.createConnection({
host: config.dbhost,
user: config.dbuser,
password: config.dbpassword,
database: config.dbname,
});
return db;
};
module.exports = {getUsersData, connectDb}
In my app I want to have:
const myModule = require("./db/lyDb");
let myData = await myModule.getUsersData({args: 'my argument'});
The problem is that I receive the following error await is only valid in async function
How to fix it? Tnx