【问题标题】:Datatables with Node JS Express and PostgreSQL带有 Node JS Express 和 PostgreSQL 的数据表
【发布时间】: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


    【解决方案1】:

    缺少在 UI 上显示数据的步骤。由于您使用的是ejs,您可以将其用作:

    1. npm 包 - npm install ejs
    2. cli - ejs ./template_file.ejs -f data_file.json -o ./output.html
    3. 浏览器 - &lt;% if (user) { %&gt;&lt;h2&gt;&lt;%= user.name %&gt;&lt;/h2&gt;&lt;% } %&gt;

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-25
    • 2020-07-01
    • 2019-04-08
    • 2016-06-25
    • 2021-06-21
    • 2021-12-22
    • 2015-08-04
    • 2013-12-16
    相关资源
    最近更新 更多