I need to get query values which will be routed to ejs file. I am able to do that using data:rows. However, this gives solution:Sandy. I only need Sandy. Below, I am pushing the values i.e. rows[i] using for loop into array arr, however, only the last element stays in the array. I tried using forEach loop which is giving syntax error. Any ideas/help would be highly appreciated!
main.js
const express = require('express')
const app = express()
var router = express.Router()
app.set('view engine', 'ejs');
var arr = [];
var mysql = require('mysql')
var connection = mysql.createConnection({
host : 'host',
port : 'port',
user : 'user',
password : 'password',
database : 'database'
});
connection.connect()
connection.query('SELECT customer_name as solution FROM customers', function (err, rows, fields) {
if (err) throw err
for(var i in rows){
arr.push[rows[i].solution];
}
app.get('/', function(req, res) {
res.render('index', {data:arr});
});
})
app.listen(3000, () => console.log('Example app listening on port 3000!'));
index.ejs
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="icon" href="images/favicon.png">
</head>
<body>
<<h1><%= data %></h1>
</body>
</html>