-1

I have a problem on my program on node.js, I want to return the result of a mysql request here is the code. Please help me

var pays = require("../entity/pays");
var bd = require('../config/bd');
var connection = bd.connection();

var PaysDao = function PaysDao() {
}

PaysDao.prototype.getAll = function() {
    var pays;
    var query = connection.query('SELECT * FROM pays ORDER BY nomPays', function (error, results, fields) {
        if (error) throw error;
        pays = results;
    });
    return pays;
}
exports.PaysDao = PaysDao;
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CertainPerformance Nov 12 '18 at 10:10

1 Answers1

0

used async await like

PaysDao.prototype.getAll = async function() {
    var pays;
   await var query = connection.query('SELECT * FROM pays ORDER BY nomPays', function (error, results, fields) {
        if (error) throw error;
        pays = results;
    });
    return pays;
}

you can use try catch for err handling

like

     PaysDao.prototype.getAll = async function() {
            var pays;
try{
           await var query = connection.query('SELECT * FROM pays ORDER BY nomPays', function (error, results, fields) {
                if (error) throw error;
                pays = results;
            });
            return pays;
        }
}catch(err){
console.log(err)
}
Abdullah Khan
  • 649
  • 5
  • 11