Skip to main content
Ortografía
Origen Enlace
jachguate
  • 32.1k
  • 8
  • 41
  • 69

El siguiente codigo ecódigo de la vercionversión (1) funciona perfectamente y tambientambién en la vercionversión (3) Sin.

Sin embargo al tratar de exportar desde un archivo externo consumiendo mysql2/promise memysql2/promise me dice que Callback function is not available with promise clients. porque

Callback function is not available with promise clients.

¿Por qué? no

No encuentro diferencia entre exportar desde database.js oo usarla directamente en querys.js

El siguiente codigo e la vercion (1) funciona perfectamente y tambien en la vercion (3) Sin embargo al tratar de exportar desde un archivo externo consumiendo mysql2/promise me dice que Callback function is not available with promise clients. porque? no encuentro diferencia entre exportar desde database.js o usarla directamente en querys.js

El siguiente código de la versión (1) funciona perfectamente y también en la versión (3).

Sin embargo al tratar de exportar desde un archivo externo consumiendo mysql2/promise me dice que

Callback function is not available with promise clients.

¿Por qué?

No encuentro diferencia entre exportar desde database.js o usarla directamente en querys.js

se eliminaron 182 caracteres en el cuerpo
Origen Enlace
Rodrigo
  • 6.2k
  • 6
  • 16
  • 37

//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;

//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;
Origen Enlace
nestdan
  • 1.3k
  • 1
  • 8
  • 14

Error al interactuar con MYSQl utilizando mysql2/promise async/await en node.js

El siguiente codigo e la vercion (1) funciona perfectamente y tambien en la vercion (3) Sin embargo al tratar de exportar desde un archivo externo consumiendo mysql2/promise me dice que Callback function is not available with promise clients. porque? no encuentro diferencia entre exportar desde database.js o usarla directamente en querys.js

//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;