0

I am trying to connect mysql with node.js but I got an undefined when I do console.log(result). This is my code:

dbConnection.js

const mysql = require('mysql');

module.exports = () => {
return mysql.createConnection({
        host: 'localhost', 
        user: 'root',
        password: 'root',
        database: 'news_portal'
    });
}

news.js

const dbConnection = require('../../config/dbConnection');

module.exports = app => {
    const connection = dbConnection();
    app.get('/', (req, res) => {
        connection.query('SELECT * FROM news', (err, result) => {
            console.log(result);
        });
    });
}

The database has info, and the user and password is correct. Can someone help me? Thanks.

vrintle
  • 5,501
  • 2
  • 16
  • 46
Helguera
  • 85
  • 1
  • 8

1 Answers1

0

I finally solve it. err var contained Client does not support authentication protocol requested by server; consider upgrading MySQL client. And I found a solution to that error in this post

Basically what I did was:

  1. use mysql;
  2. ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_MYSQL_PASSWORD'

Thanks for the help.

Helguera
  • 85
  • 1
  • 8