I am trying to make a function that grabs all rows inside a database table (platforms), which inserts every row name (platform_name) in an array. This should end up outputting a list of rows in my HTML document.
Here is the relevant code:
var getData = new function() {
this.query = 'SELECT * FROM platforms';
this.exe = function() {
// Connecting to MySQL Database
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'pass',
database: 'chatter'
});
var platforms = new Array; // Creating array to return
// Getting the rows from the database
connection.query(this.query, function(err, rows, fields) {
// Adds every row to the platforms array
for(var i in rows) {
platforms.push(String(rows[i].platform_name));
}
});
return platforms; // Returns array
connection.end();
}
}
<script> console.log(getData.exe()); </script>
<script> getData.exe(); </script>
When I run the function getData.exe() inside console.log it outputs:
And when I run the function just as it is then it does not return anything... surprise!
I am kind of stuck with this problem, so I hope that a friendly soul have the energy to take a look at this. I am practically new to javascript because I would like to learn to make native webs with node.js.