0

Express API with mysql2 I want to use the async and await to query the data from the product table but nothing print at all, don't know how to use it properly.

exports.getAllTours = function(req, res) {
  getTours()
  .then(data => console.log(data))
  .catch(err => console.log(err ));
}

async function getTours() {
    var sql = "Select * from product_prd"
    return new Promise(async function(resolve, reject) {
        let [rows, fields] = await poolQuery(sql)
        setTimeout(function() {
            resolve(rows);
        }, 500);
    })
    .catch(err => reject(err));
}

async function poolQuery(sql, args) {
    return new Promise((resolve, reject) => {
        promisePool.query(sql, args, (err, rows) => {
            if (err)
                return reject(err);
            resolve(rows);
        }).catch(err => reject(err));
    });
}

I created the pool of connection like this by following the official documentation of mysql2

const mysql = require('mysql2');

const pool = mysql.createPool({
    host: 'localhost',
    user: 'super',
    port: '3307',
    password: 'sohail',
    database: '784413_wonder',
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0

});
// now get a Promise wrapped instance of that pool
const promisePool = pool.promise();

module.exports = promisePool;
Sohail
  • 587
  • 7
  • 25
  • What are you trying? What isn't working? – Guerric P Oct 19 '18 at 11:55
  • @YoukouleleY I want to query the data from the product table in async mode but nothing print at all – Sohail Oct 19 '18 at 11:58
  • Well your `getAllTours` function has two parameters, `req` and `res`, the first thing you have to do is add resuts in `res` using `res.send(yourData);` – Guerric P Oct 19 '18 at 12:01
  • will do this but why it's not printing anything – Sohail Oct 19 '18 at 12:03
  • Why are you wrapping `promisePool.query` into a `Promise` if it already returns one and you don't add logic at all? – Guerric P Oct 19 '18 at 12:08
  • @YoukouleleY by removing promise from promisePool.query it says UnhandledPromiseRejectionWarning: TypeError: (intermediate value) is not iterable at process._tickCallback (node:14019) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. – Sohail Oct 19 '18 at 12:12
  • Your error handling is wrong in `getTours`, there is no `reject` inside the `Promise` and the `.catch(err => reject(err));` has no sense, it refers to `reject` which is out of the scope. I'm pretty sure your core problem is a SQL error – Guerric P Oct 19 '18 at 12:18
  • @YoukouleleY please suggest some alternative way. how can I get the SQL error in await poolQuery(sql) – Sohail Oct 19 '18 at 12:22
  • Please read the answer I just wrote – Guerric P Oct 19 '18 at 12:25

1 Answers1

2

I'm pretty sure there is a SQL error, and you cannot figure it out because the error handling in getTours is wrong.

Try this:

exports.getAllTours = function(req, res) {
  getTours()
  .then(data => console.log(data))
  .catch(err => console.log(err ));
}

async function getTours() {
    var sql = "Select * from product_prd"
        return new Promise(async function (resolve, reject) {
            let rows;
            let fields;
            try {
                [rows, fields] = await promisePool.query(sql, args);
            } catch (err) {
                reject(err);
            }

            setTimeout(function () {
                resolve(rows);
            }, 500);
        });
}
Guerric P
  • 30,447
  • 6
  • 48
  • 86