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.