0

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:

enter image description here

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.

Jens Kvist
  • 559
  • 2
  • 7
  • 15
  • 1
    welcome to wonderful world of async. You can't do that. Your `platforms` is empty until callback of `query` is executed. Which likely doesn't happen yet when `exe` returns. – Sergio Tulentsev Mar 10 '17 at 11:50
  • 1
    Your code IS returning the desired value, hence being visible in the console.log. What are you aiming to do with the data? – Dan Mar 10 '17 at 11:50
  • 1
    it's visible in the console log because the console lies ... console log an empty array, and then push values to it ... the console shows the filled array, because it's a liar – Jaromanda X Mar 10 '17 at 11:51
  • @JaromandaX: yeah, that fact bit me more often than I'm willing to admit :) – Sergio Tulentsev Mar 10 '17 at 11:52
  • I am trying to print it out in my HTML document. The return statement stops the execution of a function and returns a value from that function. – Jens Kvist Mar 10 '17 at 11:53
  • @JaromandaX `Console lies`. That's a good one :) Best explanation ever... – RaR Mar 10 '17 at 11:55
  • Hover over the `i` in your console .. `Value below was evaluated just now` ... now ... not back when it was logged – Jaromanda X Mar 10 '17 at 11:55
  • @RaR - I am being tongue in cheek - it logs exactly what you tell it to log - you just have to understand that what you are looking at is not an immutable "snapshot" – Jaromanda X Mar 10 '17 at 11:57
  • @JaromandaX Yeah I know. And sorry I didn't mean to say it's right or wrong or anything. I did understand why you said that. Once I had a great problem with that dynamic change of values on console... What you said was really humorous :) – RaR Mar 10 '17 at 12:04

2 Answers2

1

You need to assign the return value of your function in a variable for you to use it later. Calling just this function will return the data but nothing will be useful.

imprezzeb
  • 706
  • 1
  • 7
  • 18
0

You need to write the result of the getData.exe() method to the html document, something like

<script> document.write(getData.exe()); </script>
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
MikNiller
  • 1,242
  • 11
  • 17