John John - 3 months ago 25
MySQL Question

throw err; ReferenceError: document is not defined

Router page

var express = require('express');
var router = express.Router();
var mysql = require('mysql');

/* GET home page */
router.get('/', function(req, res, next) {
res.render('mysql', {
title: '',
});
});

var connection = mysql.createConnection({
host : '',
user : '',
password : '',
database : ''
});

connection.connect();
connection.query('SELECT hashtag from recipients', function(err, rows, fields) {
if (!err) {
console.log(rows);
document.getElementById('recipients').innerHTML = rows;
}
else {
console.log('Error while performing Query.');
}

});
connection.end();
module.exports = router;


Views page

<html>
<head>

</head>
<body>
<p id='recipients'></p>
<p id='error'></p>

<script>
</script>

</body>

AJS AJS
Answer

Please do your research before you start using a platform. Node.js may be java-script but there is a major difference between Node.js and conventional java-script used in browser. Node.js is is a server side platform much like php or python that you use to write web application or any other type of application, Javascript that runs on browser is client side used to manipulate DOMs and for many other purposes. Now there is plenty of things you did wrong.

var express = require('express');
var router = express.Router();
var mysql = require('mysql');


/* GET home page */
router.get('/', function(req, res, next) {
  var connection = mysql.createConnection({
    host: '',
    user: '',
    password: '',
    database: ''
  });

  connection.connect();

  connection.query('SELECT hashtag from recipients', function(err, rows, fields) {
    if (!err) {
      console.log(rows);
      //send rows to template engine to render HTML.
      res.render('mysql', {
        rows: rows,
      });
    } else {
      console.log('Error while performing Query.');
    }
  });
  connection.end();
});
<html>

<head>
  <title>something here</title>
</head>

<body>
  <table>
    <tr>some heading</tr>
    <% for(var i=0; i< rows.length; i++) {%>
      <tr>
        <td>
          <!-- row[i].key access some value from object -->
         
          <%= row[i].someKey%>
        </td>
      </tr>
      <% } %>
  </table>
</body>

</html>

Now i have made a assumption that you use EJS as your template engine. And this is not a working example just a snippet on how this should work. May be this tutorial helps with what you are trying to do. You should read this

Comments