【发布时间】:2021-10-07 21:17:48
【问题描述】:
我是个新手,提前感谢您的意见。我为自己设定了一项艰巨的任务,即构建我的第一个全栈应用程序。我正在尝试将我的快速应用程序 PostgreSQL db 与 Datatables 链接起来。 API 会按原样在控制台中获取数据和日志,但是,该表并未显示任何数据。我觉得我少了一步。谁能帮帮我?
这是我的代码:
索引.ejs
'''
<div class="orderTable">
<table id="myTable" class="display">
<thead>
<tr>
<th>Order Id</th>
<th>order_type</th>
<th>Timeframe</th>
<th>Max Trade Size</th>
<th>Market</th>
<th>KoF Timeframe</th>
</tr>
</thead>
</tbody>-->
</table>
</div>
'''
索引.js
app.set('视图引擎', 'ejs');
//midlleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true })); //req.body
app.use(express.static(path.join(__dirname, 'assets')));
app.listen(5000, () => {
console.log("server running on port 5000")
});
//路由//
app.get('/getOrder', db.getOrder);`
orders.js
const getOrder = (request, response) => {
pool.query(
'SELECT * FROM orderbook_table')
.then(res => {
console.log(res.rows)
response.render('index', { orders: res.rows });
}).catch(error => {
if (error) {
throw error
}
})
}
前端Datatable Jquery
$(document).ready(function() {
$('#myTable').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "/getOrder",
},
"columns": [
{ 'data': "order_type" },
]
});
});
控制台日志数据
[
{
id: 186,
market: 'AAPL - Apple Inc. Common Stock ',
order_type: 'Call ',
trade_amount: '2000 ',
strike_price: null,
filled_status: null,
kof_frame: null,
time_frame: '30 mins ',
expiration_date: null,
'settles_in ': null,
timestamp: 'Mon Aug 02 2021 11:01:48 GMT+0200 (Central European Summer Time) ',
order_status: null
}
]
'''
【问题讨论】:
标签: javascript node.js postgresql express datatable