Connection Node Js Mariadb

2

Hi, I'm trying to make a connection between nodejs and mariadb through Xampp, but when I run it does not return anything, I should return the 1 generated by the query, but nothing else does not send me anything. Does anyone know where I'm fying?

// db

const mariadb = require('mariadb');


const pool = mariadb.createPool({ // Open a new connection                                                                                                                                           
    host: '192.168.64.2', 
    user:'EdAdmin', 
    password: '123456',
    database : 'TUsers',
    port:3000
});

pool.getConnection()
    .then(conn => {

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

    }).catch(err => {
      //not connected
    });
    
asked by E.Rawrdríguez.Ophanim 14.12.2018 в 21:25
source

1 answer

1

I greet you and tell you that your query should be this way; whereas:

The mariadb connector allows you to work the connection through:

  • Promises
  • Async & Await
  • For this case we will use the case of async & await

    const mariadb = require('mariadb')
    
    const conecta = mariadb.createPool({
        host: 'localhost',
        user: 'root',
        password:'pass',
        database: 'database',
        port: '3307'
    })
    
    let fetchData = async () => {
        let conecction
        try{
            conecction = await conecta.getConnection()
            const filas = await conecction.query("SELECT 1 as val")
            filas.forEach((fila) => {
                console.log(fila)
            })
        }catch(err){
            return err
        }
    }
    
    fetchData()
    

    EXPLANATION

  • We create the connection by assigning it to conecta
  • Through an asynchronous function; we get the value of the connection
  • To the variable filas we assign access to the method query to execute SELECT 1 as Val
  • By means of a forEach we go through the values that the arrangement contained in filas brings us and assign it in variable filas
  • At the end we send the function call by means of its name fetchData
  • Final result

    You should get by console, something similar to the following

    C:\Users\User\Desktop\project>node app.js
    { val: 1 }
    

    CLARIFICATION

    If you do not go to variable filas by means of a forEach ; then the console will return a result similar to the following

    C:\Users\User\Desktop\project>node app.js
    [ { val: 1 },
      meta: [ { collation: [Collation],
          columnLength: 1,
          columnType: 3,
          scale: 0,
          type: 'LONG',
          flags: 129,
          db: [Function: bound getStringProperty],
          schema: [Function: bound getStringProperty],
          table: [Function: bound getStringProperty],
          orgTable: [Function: bound getStringProperty],
          name: [Function: bound getStringProperty],
          orgName: [Function: bound getStringProperty] } ] ]
    
        
    answered by 15.12.2018 в 00:11