传递对象ReferenceError

时间:2019-06-21 12:19:52

标签: javascript node.js express ejs

最初,我试图将对象:id传递给url,但是它成功了,但是之后,当我尝试将对象传递给列表时,它开始显示ReferenceError。

index.ejs:

  <tr>
    <th class="text-center">AWB NO</th>
    <th class="text-center">Date</th>
    <th class="text-center">Supplier</th>
    <th class="text-center">Country</th>
  </tr>
</thead>
<tbody id="myTable">
  <tr>
    <td class="id"><a href="/dist" + AWB_NO>1687952</a></td>
    <td>06/06/2019</td>
    <td>Tropic Frozen</td>
    <td>Germany</td>
  </tr>

index.js:

router.get('/', function (req, res, next) {
    connection.query('SELECT * FROM orders', function (err, rows) {

        if (err) {
            req.flash('error', err);
            res.render('index', { page_title: "index - Node.js", data: '' });
        } else {

            res.render('index', { page_title: "index - Node.js", data: rows });

        }

    })
});

dist.ejs:

<li class="list-inline-item">AWB Number:<%= AWB_NO %></li> //尝试传递对象

dist.js:

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

    connection.query('SELECT * FROM deliveries', function (err, rows) {

        if (err) {
            req.flash('error', err);
            res.render('dist', { page_title: "dist - Node.js", data: '' });
        } else {

            res.render('dist', { page_title: "dist - Node.js", data: rows });

        }

    })
});

router.get('/:awb', function (req, res) {
    res.render('dist', {
     AWB_NO: req.params.awb
    })
});

错误:

ReferenceError: E:\Dev\admeghbalim\YinSeafood\YinSeafood\expressfirst\views\dist.ejs:20
    18|             <h3>Distributor information</h3><br>&nbsp;
    19|             <ul class="list-inline">
 >> 20|                 <li class="list-inline-item">AWB Number:<%= AWB_NO %></li> &nbsp;
    21|                 <li class="list-inline-item">Country:</li>&nbsp;
    22|                 <li class="list-inline-item">Date:</li>&nbsp;
    23|                 <li class="list-inline-item">Sender:</li>&nbsp;

AWB_NO is not defined

1 个答案:

答案 0 :(得分:0)

您可以进行如下更改:

index.ejs

    <tr>
    <th class="text-center">AWB NO</th>
    <th class="text-center">Date</th>
    <th class="text-center">Supplier</th>
    <th class="text-center">Country</th>
  </tr>
</thead>
<tbody id="myTable">
  <tr>
    <td class="id"><a href="/dist/<%= AWB_NO %>">1687952</a></td>
    <td>06/06/2019</td>
    <td>Tropic Frozen</td>
    <td>Germany</td>
  </tr>

在index.js中,我假设您传递了AWB_NO的值。假设您有任何数组或对象,而不是可以使用for循环进行迭代并像array ['AWB_NO']一样编写的对象。它取决于您如何在render函数中传递值。 更新了dist.js:

  router.get('/dist/:awb',  function (req, res) {
        res.render('dist', {
            AWB_NO: req.params.awb
        })
});

dist.ejs:

<li class="list-inline-item">AWB Number:<%= AWB_NO %></li>

希望您能够解决问题。