简体   繁体   中英

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>
// db.js
var connection = mysql.createConnection({
    host : '',
    user : '',
    password : '',
    database : ''
});
connection.connect();
module.exports = connection;

...
// router.js
var db = require('db.js')

router.get('/', function(req, res, next) {
    db.query('SELECT hashtag from recipients', function(err, rows, fields) {
        if (err) {
            return next(err); // or res.send(err.message);

        res.render('mysql.html', {
            title: '',
            recipients : rows   
        });
    }); 
});

module.exports = router;

// mysql.html
<html>
<head>
    <title>{{title}}</title>
</head>
<body>
    {{recipients}}
</body>
</html>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM