I have just started working with Node.js and need to retrieve some data from a database. That has thrown me into the world of promises and callbacks, which I having difficulty getting to work.
I am creating a connection pool, which works fine:
const mysql = require('mysql2');
var database = mysql.createPool({
host: "localhost",
user: "user",
password: "password",
database: "db"
});
Then my research has suggested that the following code should wrap the resolved database query into a function. This seems to work:
var query = "SELECT * FROM table;";
async function getResult(){
const res = database.query(query);
return res[0];
};
Finally, I want to consume the result and use it in the remaining program. However, this just returns an unresolved promise:
var result = getResult(); // This should be available to the global scope.
console.log(result); // Promise { <pending> }
I don't understand how to resolve the promise and actually get the data I need.
If I try to add an await in the function, I get an error:
async function getResult(){
const res = await database.query(query);
return res[0]; // ERROR
};
If I try to add an await to the function output, I also get an error:
var result = await getResult(); // ERROR
I feel there should be a simple step that I am missing.
EDIT:
The errors I get are; for the await in the function:
You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface. To learn how to use async/await or Promises check out documentation at https://sidorares.github.io/node-mysql2/docs#using-promise-wrapper, or the mysql2 documentation at https://sidorares.github.io/node-mysql2/docs/documentation/promise-wrapper /var/www/html/nodejs/node_modules/mysql2/lib/commands/query.js:43 throw new Error(err); ^
Error: You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface. To learn how to use async/await or Promises check out documentation at https://sidorares.github.io/node-mysql2/docs#using-promise-wrapper, or the mysql2 documentation at https://sidorares.github.io/node-mysql2/docs/documentation/promise-wrapper at Query.then (/var/www/html/nodejs/node_modules/mysql2/lib/commands/query.js:43:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.17.1
and for the await outside the function:
SyntaxError: await is only valid in async functions and the top level bodies of modules at internalCompileFunction (node:internal/vm:73:18) at wrapSafe (node:internal/modules/cjs/loader:1178:20) at Module._compile (node:internal/modules/cjs/loader:1220:27) at Module._extensions..js (node:internal/modules/cjs/loader:1310:10) at Module.load (node:internal/modules/cjs/loader:1119:32) at Module._load (node:internal/modules/cjs/loader:960:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47
Node.js v18.17.1
getResult()returns a promise, and you must wait for the result. There's no way around this.awaits you have placed seem appropriate. But unless you tell us what error they "caused" (exposed), we can't tell you what the problem is or how to fix it.await getResult()call inside anasync function, where were you trying to use it?