Query with Node Js and MariaDb

0

I'm trying to do a query with Node and MariaDb , but I'm getting this error:

  

GET / - - ms - -   (node: 3244) UnhandledPromiseRejectionWarning: Error: pool is closed       at Object.module.exports.createError (/Users/dmn/Desktop/apirestIntrost/node_modules/mariadb/lib/misc/errors.js:55:10)       at Pool.end (/Users/dmn/Desktop/apirestIntrost/node_modules/mariadb/lib/pool.js:54:16)       at pool.query.then.then.catch.err (/Users/dmn/Desktop/apirestIntrost/src/models/user.js:33:18)

This is the model I want to make and this is my route.

user.js

const mariadb = require('mariadb');
const pool = mariadb.createPool({
     host: 'x.x.x.x', 
     user:'x', 
     password: 'x',
     connectionLimit: 5
});

if(!pool){
    console.log('conección falló');

}else{
    console.log('good conección');   
}

let userModel = {};

userModel.getUsers = (callback) => {
    pool.getConnection()
    .then(conn => {

      conn.query('SELECT * FROM 'users order by id'')
        .then((rows) => {
          console.log(rows); //[ {val: 1}, meta: ... ]
          //return conn.query("INSERT INTO myTable value (?, ?)", [1, "mariadb"]);
          callback(rows);
        })
        .then((res) => {
          console.log(res); // { affectedRows: 1, insertId: 1, warningStatus: 0 }
          conn.end();
        })
        .catch(err => {
          //handle error
          conn.end();
        })

    }).catch(err => {
      //not connected
    });
};


module.exports = userModel;

userRoutes.js

const User = require('../models/user');


module.exports = (app)=>{
    app.get('/',(req,res)=>{
        User.getUsers((err,data) => {
            res.status(200).json(data);
        });  
    });

} 
    
asked by E.Rawrdríguez.Ophanim 06.12.2018 в 17:01
source

1 answer

2

As I see in the code you show, I see a possible error that may help you solve the problem you have, maybe it is not the correct solution but I would like you to try it since the 'Warning' that shows you in the code is related to the Promise and what I want to show you is related to it, I comment.

I think the problem is in the method 'mariadb.createPool ()', this method returns a Promise that you have to solve it so that it does not throw the error, therefore in the first '.then ()' do not return any data does not solve it and then it will not pass to the second 'then ()', so it will not close the connection and that's why it sends you that 'Warning', warning you that the connection is not closed correctly.

I pass the code that I think will work for you (as long as the connection options are correct):

user.js

const mariadb = require('mariadb');
const pool = mariadb.createPool({
     host: 'x.x.x.x', 
     user:'x', 
     password: 'x',
     connectionLimit: 5
});

module.exports.getUsers = () => {
   return pool.getConnection()
              .then(conn => {
                    return conn.query('SELECT * FROM 'users order by id'')
              .then(rows => {
                    conn.end()
                    return rows
              })
              .catch(err => {
                    console.log(err.message)
                    conn.end()
                    return err
              })
};

app.js

const User = require('../models/user');

module.exports = (app)=>{
    app.get('/',(req,res)=>{
        User.getUsers()
            .then(data => res.status(200).json(data))
            .catch(err => res.json(err)) //Tratar el error como desees.
    });
} 

Confirm me if it worked.

Greetings

    
answered by 06.12.2018 в 21:14