-1

I need to add a JavaScript file in HTML code and need to get the output of the added JavaScript file in the HTML page. Please help me, I'm stuck and this is my HTML code:

<!DOCTYPE html>
<html>
    <body>
        <script src="tst.js">
        </script>
    </body>
</html>

And this is my JavaScript code:

var mysql = require('mysql');
var connection = mysql.createConnection({
    host : 'host',
    user : 'user',
    password : 'pass',
    database : 'db',
    insecureAuth : 'true'
});
connection.connect();
var json = '';
connection.query('SELECT * FROM SmartAG AS readings ORDER BY Timestamp DESC', function(err, rows, fields) {
    if (err) throw err;
    console.log('The solution is: ');
    console.log(rows[0]); // results are coming here.
    json = JSON.stringify(rows[0]);
    console.log(json);
    //output.innerHTML = JSON.stringify(json)
});

connection.end();

I am not getting any output on the HTML page with the added script file. Can you people help me?

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143

1 Answers1

0

You get an error in the console when you run your code:

Uncaught ReferenceError: require is not defined

I've changed the js file to only contain alert("Test"); and it ran fine.

Its the require method you use that isn't JavaScript thats Node.js I believe.

Edit: After a bit of research it seems as though there are multiple third party require functions so it should be the case of finding the one your after and including that library

Mojo Jojo
  • 16
  • 4