Answer the question
In order to leave comments, you need to log in
How exactly do I make this (code in the text) Node.js function asynchronous?
Hello. Here is the function from serverMiddleware in Nuxt:
export default function(req, res, next) {
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'secret',
database: 'db'
});
connection.connect();
connection.query("SELECT * FROM table", function(err, result, fields) {
if (err) throw err;
let exp = JSON.stringify(result);
res.send(exp);
});
connection.end();
}
Answer the question
In order to leave comments, you need to log in
You already have everything asynchronous. You just close the connection immediately without waiting for the request to complete.
take out the creation and closing of the connection to the database from the middleware somewhere in the server initialization and you will have everything as it should be.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question