I am using express
and mysql
packages to just test out queries.
I had this code:
connection.connect();
connection.query('SELECT 1 + 1 AS solution', (error, results) => {
if (error) {
connection.end();
console.log('error');
throw error;
}
connection.end();
console.log(results);
return results;
});
And it works just fine. But when I move it to a different file and try to export it like this:
query.js
const connection = require('../config/connection');
connection.connect();
module.exports = async (query) => connection.query(query, (error, results) => {
console.log(query);
if (error) {
// connection.end();
console.log('error');
throw error;
}
// connection.end();
console.log(results);
return results;
});
app.js
try {
const queryResult = await query('SELECT 1 + 1 AS solution');
console.log(queryResult, 2);
res.send(queryResult);
} catch (err) {
console.log(err);
}
I get this error:
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at stringify (C:\Users\aironsid\Documents\tatooify\server\node_modules\express\lib\response.js:1119:12)
at ServerResponse.json (C:\Users\aironsid\Documents\tatooify\server\node_modules\express\lib\response.js:260:14)
at ServerResponse.send (C:\Users\aironsid\Documents\tatooify\server\node_modules\express\lib\response.js:158:21)
at app.get (C:\Users\aironsid\Documents\tatooify\server\app.js:44:9)
at process._tickCallback (internal/process/next_tick.js:68:7)
Also in console logs I get, a big object followed by number 2, then I get the errthe ``or, then I get the query value, and then the result. Like so:
Big object 2
error
SELECT 1 + 1 AS solution
[ RowDataPacket { solution: 2 } ]
So what this tells me is that my code runs in async. Removing async/await from both files doesn't change anything. So my code first runs the app.js
code, then the query.js
code, which takes some time. But why? And how can I make it wait? connection.query()
does not return a promise.
Problem is that your async
function does not wait for query to finish.
It should return a promise that resolves when the query completes, but instead returns promise that immediately resolves into return value of function connection.query()
.
You should change that to:
module.exports = (query) => {
return new Promise(resolve =>
connection.query(query, (error, results) => {
console.log(query);
if (error) {
// connection.end();
console.log('error');
throw error;
}
// connection.end();
console.log(results);
resolve(results);
})
);
}
With omited commented lines and shorter syntax:
module.exports = (query) => new Promise((resolve, reject) =>
connection.query(query, (error, results) => {
if (error) return reject(error);
resolve(results);
})
);
And even shorter:
module.exports = (query) =>
new Promise((resolve, reject) =>
connection.query(query, (error, results) =>
error ? reject(error) : resolve(results)
)
);
To clarify: your async
keyword did not have any effect, because what it does is allow you to use await in its function and wraps whatever you return into a promise.
You did not use any await
s and immediately returned result via lambda function.
Note that your return statement returned from callback passed to connection.query function and not from the main function.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments