[英]Display data from database (using mongodb) in hbs/html file Node.Js
[英]Display data from a SQL database in an HTML table using node js/javascript
所以我创建了一个 HTML 文件,它有一个包含几列的表格。 我需要做的是找到一种方法将我的 SQL 数据库中的数据显示到这个 HTML 表中。 我已经使用 Node 创建了与数据库的连接,但我不知道如何在表中显示数据。 我到处找,但我只能找到 PHP 代码,我不想使用它。 我需要它在 Node.js 中。 我该如何开始? 谢谢你们。 到目前为止,这是我对 HTML 的了解:
<div id="table">
<table align="right" cellspacing="0" cellpadding="5">
<tr>
<th>Match </th>
<th>Time</th>
<th>Red 1</th>
<th>S</th>
<th>Red 2</th>
<th>S</th>
<th>Red 3</th>
<th>S</th>
<th>Blu 1</th>
<th>S</th>
<th>Blu 2</th>
<th>S</th>
<th>Blu 3</th>
<th>S</th>
<th>Red Sc</th>
<th>Blu Sc</th>
</tr>
</table>
</div>
关于 Javascript 文件,我已经创建了一个与 Azure 数据库的连接和一个用于从中检索数据的 SELECT 查询。 现在,我只需要将我拥有的数据放在上面的 HTML 表格中。
举个例子,你就明白了。
节点:
var express = require('express')
var app = express()
app.get('/getData', function(req, res) {
... // Get data from database
})
html:
<table>
<thead>
<th>id</th>
<th>name</th>
</thead>
<tbody id="place-here"></tbody>
</table>
<script>
$.get('/getData', {}, function(result) {
let res = '';
result.forEach(data => {
res += `<tr><td>${data.id}</td><td>${data.something}</td></tr>`
})
$('#place-here').html(res)
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.