//En la raiz app,js solo codigo de esta consulta
......
const express = require('express');
const path = require('path');
const actorsRouter = require ('./routes/actors')
const app = express();
.............
....
app.use(express.json());
app.use(express.urlencoded({ extend//
........
..........................................................
// dentro carpeta route archivo actors.js
const {Router} =require ('express');
const db = require ('../querys');
const router =Router();
router.get('/',async (req,res) => {
const query =await db.executeQuery();
return res.status (201).json(query)
})
module.exports = router;
..........................................................
// en raiz archivo querys.js
//(1)!!!!!
const mysql=require('mysql2/promise');
const pool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'TU PASS',
database : 'sakila',
waitForConnections : true,
connectionLimit : 3,
maxIdle : 3, // max idle connections, the default value is the same as connectionLimit
idleTimeout : 60000, // idle connections timeout, in milliseconds, the default value 60000
queueLimit : 0,
enableKeepAlive : true,
keepAliveInitialDelay : 0,
port: "3307",
});
async function executeQuery() {
const connection = await pool.getConnection();
console.log('connection :',connection)
console.log('Connected to the MySQL server.');
const [rows,fields] = await pool.query('SELECT * FROM sakila.actor');
return rows;
}
exports.executeQuery = executeQuery;
// responde adecuadamente a GET http://localhost:3000/actors/
.................................................
/*Codigo anterior funciona sin problemas pero si trato de usar la promesa pool
desde un archivo database.js
externo
*/
// database.js
// (2) !!!!!!
const pool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'tu pass',
database :'sakila',
port: '3307',
connectionLimit : 15
});
exports,pool = pool;
//luego al tratar de usar a pool desde querys,js con el siguiente codigo
const pool = require('./database').pool;
async function executeQuery() {
const connection = await pool.getConnection();
console.log('connection :',connection)
console.log('Connected to the MySQL server.');
const [rows,fields] = await pool.query('SELECT * FROM sakila.actor');
return rows;
}
exports.executeQuery = executeQuery;
/*
Callback function is not available with promise clients.
Error: Callback function is not available with promise clients.
at PromisePool.query (C:\Personal\CreateConnection\node_modules\mysql2\promise.js:358:13)
at C:\Personal\CreateConnection\routes\actors.js:9:10
at Layer.handle [as handle_request] (C:\Personal\CreateConnection\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Personal\CreateConnection\node_modules\express\lib\router\route.js:149:13)
at Route.dispatch (C:\Personal\CreateConnection\node_modules\express\lib\router\route.js:119:3)
at Layer.handle [as handle_request] (C:\Personal\CreateConnection\node_modules\express\lib\router\layer.js:95:5)
at C:\Personal\CreateConnection\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Personal\CreateConnection\node_modules\express\lib\router\index.js:346:12)
at next (C:\Personal\CreateConnection\node_modules\express\lib\router\index.js:280:10)
at Function.handle (C:\Personal\CreateConnection\node_modules\express\lib\router\index.js:175:3)
*/
// si en lugar de consumir mysql2/promise desde querys.js requiero solo mysql2
// Tambien como en (1) !!!! funciona sin errores
// vecion (3) !!!
//solo requiereo mysql2
const mysql = require('mysql2');
var pool= mysql.createPool({
host : 'localhost',
user : 'root',
password : 'tu pass',
database :'sakila',
port: '3307',
connectionLimit : 15
});
exports.pool = pool;
//En la raiz app,js solo codigo de esta consulta
const express = require('express');
const path = require('path');
const actorsRouter = require ('./routes/actors')
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extend//
// dentro carpeta route archivo actors.js
const {Router} =require ('express');
const db = require ('../querys');
const router =Router();
router.get('/',async (req,res) => {
const query =await db.executeQuery();
return res.status (201).json(query)
})
module.exports = router;
// en raiz archivo querys.js
//(1)!!!!!
const mysql=require('mysql2/promise');
const pool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'TU PASS',
database : 'sakila',
waitForConnections : true,
connectionLimit : 3,
maxIdle : 3, // max idle connections, the default value is the same as `connectionLimit`
idleTimeout : 60000, // idle connections timeout, in milliseconds, the default value 60000
queueLimit : 0,
enableKeepAlive : true,
keepAliveInitialDelay : 0,
port: "3307",
});
async function executeQuery() {
const connection = await pool.getConnection();
console.log('connection :',connection)
console.log('Connected to the MySQL server.');
const [rows,fields] = await pool.query('SELECT * FROM sakila.actor');
return rows;
}
exports.executeQuery = executeQuery;
// responde adecuadamente a GET http://localhost:3000/actors/
/*Codigo anterior funciona sin problemas pero si trato de usar la promesa pool
desde un archivo database.js
externo
*/
// database.js
// (2) !!!!!!
const pool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'tu pass',
database :'sakila',
port: '3307',
connectionLimit : 15
});
exports,pool = pool;
//luego al tratar de usar a pool desde querys,js con el siguiente codigo
const pool = require('./database').pool;
async function executeQuery() {
const connection = await pool.getConnection();
console.log('connection :',connection)
console.log('Connected to the MySQL server.');
const [rows,fields] = await pool.query('SELECT * FROM sakila.actor');
return rows;
}
exports.executeQuery = executeQuery;
Callback function is not available with promise clients.
Error: Callback function is not available with promise clients.
at PromisePool.query (C:\Personal\CreateConnection\node_modules\mysql2\promise.js:358:13)
at C:\Personal\CreateConnection\routes\actors.js:9:10
at Layer.handle [as handle_request] (C:\Personal\CreateConnection\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Personal\CreateConnection\node_modules\express\lib\router\route.js:149:13)
at Route.dispatch (C:\Personal\CreateConnection\node_modules\express\lib\router\route.js:119:3)
at Layer.handle [as handle_request] (C:\Personal\CreateConnection\node_modules\express\lib\router\layer.js:95:5)
at C:\Personal\CreateConnection\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Personal\CreateConnection\node_modules\express\lib\router\index.js:346:12)
at next (C:\Personal\CreateConnection\node_modules\express\lib\router\index.js:280:10)
at Function.handle (C:\Personal\CreateConnection\node_modules\express\lib\router\index.js:175:3)
// si en lugar de consumir mysql2/promise desde querys.js requiero solo mysql2
// Tambien como en (1) !!!! funciona sin errores
// vecion (3) !!!
//solo requiereo mysql2
const mysql = require('mysql2');
var pool= mysql.createPool({
host : 'localhost',
user : 'root',
password : 'tu pass',
database :'sakila',
port: '3307',
connectionLimit : 15
});
exports.pool = pool;