node-mysql2: Async/await not working?

I try this:

    let [one, two] = await connection.execute('SELECT EXISTS(SELECT 1 FROM bashbase.users WHERE email=?) AS "exists";', [req.body.email])

    console.log(one, two)

(node:19626) UnhandledPromiseRejectionWarning: TypeError: (intermediate value) is not iterable

when I try to add a .then, it says connection.execute.then is not a function

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 19 (6 by maintainers)

Most upvoted comments

what about returning a Promise if no callback is provided?

@AdrianHastro I guess @SinghRajKr question was “is this example in top level scope? If yes, it won’t work because node does not have top level await yet” - see https://stackoverflow.com/questions/46515764/how-can-i-use-async-await-at-the-top-level

const mysql = require(‘mysql2/promise’); const conn = await mysql.createConnection({ database: bashbase }); const [rows, fields] = await conn.execute(‘SELECT EXISTS(SELECT 1 FROM bashbase.users WHERE email=?) AS “exists”;’, [req.body.email]); await conn.end();