简体   繁体   中英

undefined result in mysql query with node.js

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.

I finally solve it. err var contained Client does not support authentication protocol requested by server; consider upgrading MySQL client 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM