I was trying to connect AWS RDS with my Node JS application. I have tested connection of RDS with MySQLWorkbench and the data could be fetched. However when trying to get a connection in my Node application, I got an error:
TypeError: Cannot read property 'query' of undefined
My Node JS code is as follows:
const express = require('express');
const cors = require('cors');
const mysql = require('mysql');
const app = express();
const SELECT_ALL_QUERY = 'SELECT * FROM `my-schema`.`my-table`;';
app.use(cors());
app.get('/', (req, res) => {
res.send('go to /my-table to see contents')
});
const pool = mysql.createPool({
connectionLimit: 10,
host: "process.env.myHostname",
user: 'process.env.myUsername',
password: 'process.env.myPassword',
port: "process.env.3306",
database: 'my-schema',
debug: false
});
pool.getConnection((err, connection) => {
app.get('/my-table', (req, res) => {
console.log(connection);
connection.query(SELECT_ALL_QUERY, (err, results) => {
if (err) {
return res.send(err)
}
else {
return res.json({
data: results
})
};
});
});
});
let port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`App running on port ${port} `);
});
By the look of it, the connection was undefined, did it fail to connect? If so why could I see the data in MySQLWorkbench?