how do i export mysql createPool( in node.js using ES7 format

31 Views Asked by At

Trying to learn how to export in Node.js using ES7 format Specifically mySql and getConnection() {

FILE: connection.mjs file.

    import mysql from 'mysql';

    var ip="";

    // i had the const before and working in my monolitic file.. but now want separate modules/files.
    //export const pool = mysql.createPool({
    var pool = mysql.createPool({
        connectionLimit: 10,
        port: '3306',
        //user: 'root',
        user: 'smsqluser',
        password: process.env.SECRET_KEY, 
        host: 'localhost',
        database: 'mydb', 
        multipleStatements: true
      })
 
    function getConnection() {
        //console.log("function: getConnection")
        return pool
      }
  
    //ES7 format
    export default getConnection;
    export { getConnection };

FILE: router.mjs contents: express routers. the routers also include mySQL selects, but the results of the query are not returning rows. This works in monolithic single file

    getConnection().query(queryString, (err, rows, fields) => {
        console.log(routerName+" START: query was run");
        console.log(routerName+" SQL:   " + queryString)
          
        if (err) {
            console.log(routerName+"Failed to query from netnodes: " + err)
            res.sendStatus(500)
            return
        }


        console.log(routerName+"server list = " + rows.IPAddress4);

nothing is contained within my rows variable

TERMINAL: routerName: server list = undefined

0

There are 0 best solutions below