How do I make a query about another MySQL query with Node.js?

0

I have created a pool of MySQL

module.exports = () => {
return mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'gym'
});

}

I have also made a query that shows me results

const dbConnection = require("../modelos/config.connection");
const connection = dbConnection();
connection.getConnection(function(error, conn){
    if(error)
    {
        console.log(error);
    } // consulta que muestra los datos
    connection.query('SELECT SQL_CALC_FOUND_ROWS * FROM inventario LIMIT ? , ? ', [consulta.INICIO, consulta.ARTICULOS_POR_PAGINA], function(error, articulos)
        {
            if(error)
            {
                throw error;
                connection.release();
                res.status(500).send({message: error});
            }else
            {
                connection.query('SELECT FOUND_ROWS() as total', function(error, result)
                {
                    if(error)
                    { throw error; } 
                    else {
                        var total = result[0].total;
                        res.status(200).send({total})
                }

            }
        );
    })
connection.end();

My problem starts now that the console is dialing%% error. plus another problem that says Pool is closed . so ... how can I do this query? since what I got from the first query I want to get the total of rows to be able to make a page based on it. I hope someone can help me.

    
asked by Christian Eduardo Amaya Rivas 08.06.2018 в 19:03
source

1 answer

0

I'm seeing that you're closing the connection at the end:

connection.end();

I would eliminate that. Just make a release, something like this:

const dbConnection = require("../modelos/config.connection");
const connection = dbConnection();
connection.getConnection(function(error, conn){
    if(error)
    {
        console.log(error);
    } // consulta que muestra los datos
    connection.query('SELECT SQL_CALC_FOUND_ROWS * FROM inventario LIMIT ? , ? ', [consulta.INICIO, consulta.ARTICULOS_POR_PAGINA], function(error, articulos)
        {
            if(error)
            {
                throw error;
                connection.release();
                res.status(500).send({message: error});
            }else
            {
                connection.query('SELECT FOUND_ROWS() as total', function(error, result)
                {
                    if(error)
                    { throw error; } 
                    else {
                        var total = result[0].total;
                        connection.release();
                        res.status(200).send({total})
                }

            }
        );
    })
    
answered by 11.06.2018 в 03:58