3

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

red
  • 1,529
  • 1
  • 12
  • 33
  • Thank you to all, I's my fault, I forgot to use async keyword in my listener, but your answers has helped me to figure the problem: ``` socket.on("connection", async (_socket) => { .. }) ``` – red Sep 25 '20 at 10:20

4 Answers4

3

Can you wrap your main app code in an async function?

const myModule = require("./db/lyDb");

const main = async () => {
  let myData = await myModule.getUsersData({args: 'my argument'});
  // rest of your application
}

main()
Rashomon
  • 5,962
  • 4
  • 29
  • 67
  • OP can also use Top Level awaits based on if OP can use modules and has a ES2018 supporting Node.JS version – pratikpc Sep 25 '20 at 10:10
1

This code

let myData = await myModule.getUsersData({args: 'my argument'});

must be executed from an async function.

i.e.

async function someFunc() {

    let myData = await myModule.getUsersData({args: 'my argument'});

}
someFunc();
falinsky
  • 7,229
  • 3
  • 32
  • 56
  • There is no need in 2020 to execute awaits only from async functions especially with top level awaits – pratikpc Sep 25 '20 at 10:13
1

Error is self explanatory, you cannot call await outside a async function. Either you should use then clause to access response or create a async function to use await.

myModule.getUsersData({args: 'my argument'}).then(res => {
// do something
})

or

const main = async () => {
    let myData = await myModule.getUsersData({args: 'my argument'});
}
main();
vikash vik
  • 686
  • 5
  • 10
0

The problem in your case is that you are using a Top-Level Await

const myModule = require("./db/lyDb");

let myData = await myModule.getUsersData({args: 'my argument'});

Await can only be called in an async function in the configuration you are possibly running Node.JS

To help you out, one way is to use

const myModule = require("./db/lyDb");
(async () => {
   let myData = await myModule.getUsersData({args: 'my argument'});
})();

To actually, use Top-Level awaits, your JS must be a module and you must be targeting or running a Node.JS version which supports ES2018 with ESNext Module System

Also see How can I use async/await at the top level?

pratikpc
  • 432
  • 4
  • 12