query() is an asynchronous function from which you cannot return any results. And therefore, any functions that call the asynchronous functions themselves (for example, your findAllContinents ) cannot either.
Instead, you need to pass a callback function (also explained here ) that will be called when the request is executed:
// app.js app.get("/continents", function(request, response) { database.findAllContinents(function(err, results) { if (err) throw err; // or return an error message, or something else res.send(results); // as a demo, we'll send back the results to the client; // if you pass an object to 'res.send()', it will send // a JSON-response. }); }); // mysql.js exports.findAllContinents = function(cb) { var connection = getConnection(); connection.query('select id, code, name from Continent', function (err, rows, fields) { // close connection first closeConnection(connection); // done: call callback with results cb(err, rows); }); };
As for the (non) use of ORM, it really depends on the use case. I would choose ORM (my favorite for MySQL patio ) if my application needs several (complex) models, possibly with associations between them. In addition, the ORM abstraction makes it easier to read the code and, as a rule, allows you to more easily transfer the application to another database.
source share