【问题标题】:How to display data from MySQL into HTML-Page with NodeJS如何使用 NodeJS 将 MySQL 中的数据显示到 HTML 页面中
【发布时间】:2020-03-10 09:09:35
【问题描述】:

我是 Node JS 的新手,我正在尝试将从我的 MySQL 表中提取的数据显示到我的 HTML 文件中的表中。但我找不到任何对我有帮助的东西。如果有人可以帮助我找到解决方案,我将不胜感激! :) 这是我的 js 代码:

//app.js
// Get the mysql service
var mysql = require('mysql');
var express = require('express');
var app = express();

app.get('/', function (request , response) {
    fetchData(response);
    console.log('Done. Displayed Data.');

});
// Add the credentials to access your database
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'Breunninger',
    port: '3306'
});
// connect to mysql
connection.connect(function(err) {
    if(err){throw err;}
    console.log('Connected');
});

function executeQuery(sql, cb){
    connection.query(sql, function( result, fields){

        cb(result);
    })

}

function fetchData(response){
    executeQuery("SELECT username, tor, datum, sendungsstruktur FROM Buchung JOIN user ON(user.id = Buchung.userid)", function (result) {
        console.log(result);
        response.write('<div class="container-wrap"><table id="example" class="display"><tr>');
        for(var column in result[0]){
            response.write('<td> <label>' + column + '</label></td>');
            response.write('</tr>');
        }
        for(var row in result){
            response.write('<tr>');
            for(var column in result[row]){
                response.write('<td>' + result[row][column]+ '</td>');
            }
            response.write('</tr>');
        }
        response.end('</table></div>');
    });
}
<div class="container-wrap">
    <div class="flexslider">
        <script src="app.js"></script>
    </div>
</div>

【问题讨论】:

  • 您的示例中的哪些内容不起作用?您能否运行您的代码并提供示例输出。
  • 我没有收到错误或其他错误。控制台只显示数据库已连接。但我也想在控制台中查看结果,并且我想在 HTML 文件中显示结果(但我不知道如何,而且我在 www 中也找不到任何东西)@Max
  • 另外,您使用的是 express 而response 没有write 功能。 executeQuery 的回调应该有像 (error, results, fields) 这样的参数。你检查error的值了吗?

标签: javascript html mysql node.js express


【解决方案1】:

executeQuery 函数中有一个小(但很关键!)错误,第一个参数应该是错误对象,所以如果你可以按照下面的方式重写它,你的查询应该可以工作。

function executeQuery(sql, cb){
    connection.query(sql, function( error, result, fields){
        if (error) {
            console.error("An error has occurred:", error);
        } else {
            cb(result);
        }   
    })
}

节点回调通常为错误对象保留第一个参数,很容易错过这个!

此外,我们应该清楚,Node.js Express 代码将在服务器端运行,因此要查看结果,您需要将浏览器指向所服务的主机和端口,例如http://localhost:3000/(如果你这样做:)

app.listen(3000);

【讨论】:

  • 感谢您的回答!我使用新的 executeQuery 函数进行了尝试,但数据仍然没有显示在我的控制台中。我应该使用哪个端口?来自 Apache 服务器或我的数据库的端口?如果我使用数据库中的端口,我会得到 EADDRINUSE..
  • 您应该能够使用除数据库端口或 Apache 服务器之外的任何端口。通用端口号(用于开发目的)将是 3000、8000、8080。试试看吧!请记住,您的浏览器地址将是:localhost:port,例如localhost:3000。 (我假设你在本地运行它!)
  • 好的,我现在使用另一个端口。但是结果仍然没有显示在我的控制台中:(我真的很抱歉问了这么愚蠢的问题,但我还在学习中:-D
  • 没有愚蠢的问题。我们都一直在学习!所以您正在运行节点,然后将您的浏览器连接到您的节点实例?代码只会在 Express 服务器收到请求时运行!
  • 不,我只是启动节点应用程序以检查结果是否显示在我的控制台中
猜你喜欢
  • 2022-01-25
  • 2017-06-23
  • 1970-01-01
  • 2021-08-09
  • 2017-04-17
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
相关资源
最近更新 更多