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);
});
});
}