0

我使用带有mssql包 ( https://www.npmjs.com/package/mssql ) 的 node express 应用程序从我的 Azure SQL Server 数据库中读取数据。一般来说,我可以读取数据库中的表格,但是,我不知道如何将表格可视化到我使用把手模板作为节点的首页。

这是我的代码:

路线/index.js

const express = require('express');
const router = express.Router();
const config = require('../dbconfig');
const sql = require('mssql');

// Get homepage
router.get('/', function(req, res) {    

  // Read data rows from the database (dbo.lendbook table)
  new sql.ConnectionPool(config).connect().then(pool => {
    return pool.query `select * from dbo.lendbook`
  }).then(result => {
    res.render('index');
      // Output the data which was read in the terminal:
    //  console.dir(result);
  }).catch(err => {
    console.log(err);
  });

视图/index.handlebars

<h2 class="page-header">Dashboard</h2>
<p>Welcome to your dashboard</p>

{{#each rows}}
   <div>{{item}}</div>
{{/each}}

这是我可以通过上面的函数读取的数据库中的表

在此处输入图像描述

4

1 回答 1

0

尝试这个:

路线/index.js

new sql.ConnectionPool(config).connect().then(pool => {
    return pool.query `select * from dbo.lendbook`
}).then(result => {

    res.render('index', {
        rows: result.recordset
    });

}).catch(err => {
    console.log(err);
});

视图/index.handlebars

<h2 class="page-header">Dashboard</h2>
<p>Welcome to your dashboard</p>

<table>
    {{#each rows}}
        <tr>
            <td>{{this.id}}</td>
            <td>{{this.rate}}</td>
            <td>{{this.amount}}</td>
        </tr>
    {{/each}}
</table>
于 2017-10-05T08:21:57.143 回答