使用多个值分割字段的值

时间:2019-03-24 05:29:11

标签: node.js mongodb express mustache

我目前正在一家电子商务网站上工作,我无法在页面上呈现报告。

这是我的节点server.js

app.get('/adminReport', function (req,res){
var transaction = []
        mongoDb.collection('transactions').find().toArray(function(err,results) 
        {
            transaction =results
        //console.log(results);
            return res.render('adminReport.html', {
            transactions: transaction
            });
        });
});

这是我在HTML上的代码

<thead>
    <tr>
        <th>Transaction ID</th>
        <th>Name</th>
        <th>UserID</th>
        <th>Time Ordered</th>
        <th>Job Title</th>
        <th>Address</th>
        <th>Email Address</th>
        <th>Contact Number</th>
        <th>Purchase Option(Pick-up or Delivery)</th>
        <th>Payment Method</th>
        <th>Item Ordered</th>
        <th>Status</th>
    </tr>
</thead>

<tbody>
        {{#transactions}}
    <tr>
        <td>{{_id}}</td>
        <td>{{lastName}}, {{firstName}}</td>
        <td>{{userId}}</td>

        <td>{{timeOrdered}}</td>
        <td>{{jobTitle}}</td>
        <td>{{address}} , {{city}}, {{province}}, {{zip}}</td>
        <td>{{email}}</td>
        <td>{{contactNo}}</td>
        <td>{{purchaseOpt}}</td>
        <td>{{paymentMethod}}</td>
        <td>
                <table>
                    <th>Transaction ID</th>
                    <th>Item Name</th>
                    <th>Quantity</th>
                    <th>Price</th>
             <!-- item purchased -->
                    <tr>
                        <td>{{cartId}}</td>
                        <td>{{itemName}}</td>
                        <td>{{quantity}}</td>
                        <td>₱{{price}}</td>
                    </tr>

                </table>
            </td>
        <td>{{status}}</td>

    </tr> {{/transactions}}

这是Chrome浏览器源代码中的结果

<thead>
    <tr>
        <th>Transaction ID</th>
        <th>Name</th>
        <th>UserID</th>
        <th>Time Ordered</th>
        <th>Job Title</th>
        <th>Address</th>
        <th>Email Address</th>
        <th>Contact Number</th>
        <th>Purchase Option(Pick-up or Delivery)</th>
        <th>Payment Method</th>
        <th>Item Ordered</th>
        <th>Status</th>
    </tr>
</thead>

<tbody>
    <tr>
        <td>5c9711730d8f1c2287f5a439</td>
        <td>Jordan, Micheal</td>
        <td>12341</td>

        <td>Sunday 24 Mar 1:11 PM</td>
        <td>Programmer</td>
        <td>123 St. Atlanta , Baguio, Kadabra, 1500</td>
        <td>micheal@tesing.com</td>
        <td>95057435</td>
        <td>pickUp</td>
        <td>cash</td>
        <td>
                <table>
                    <th>Transaction ID</th>
                    <th>Item Name</th>
                    <th>Quantity</th>
                    <th>Price</th>
            <!-- item purchased -->
                    <tr>
                        <td>5c95cb8f36d0c71f05ddf9de,5c95d4b6d82a782747e929a3</td>
                        <td>Bag,Shirt</td>
                        <td>2,1</td>
                        <td>₱62,20</td>
                    </tr>
                </table>
            </td>
        <td></td>

    </tr> 

</tbody>

我想将包和衬衫分开放在不同的<td>上。

我正在使用mongoDB作为数据库,如果仅某个字段具有多个值,是否可以拆分该值?

    firstName: 'Micheal',
    lastName: 'Jordan',
    kinId: '12341',
    country: 'Deployed',
    contactNo: '95057435',
    email: 'micheal@tesing.com',
    jobTitle: 'Programmer',
    address: '123 St. Atlanta',
    province: 'Kadabra',
    city: 'Baguio',
    zip: '1500',
    purchaseOpt: 'pickUp',
    paymentMethod: 'cash',
    timeOrdered: 'Sunday 24 Mar 1:11 PM',
    cartId: [ '5c95cb8f36d0c71f05ddf9de', '5c95d4b6d82a782747e929a3' ],
    itemName: [ 'Bag', 'Shirt' ],
    quantity: [ '2', '1' ],
    price: [ '62', '20' ] } ]

关于这个问题,cartId,itemName,quantity,price是唯一具有多个值的字段,是否可以在我的HTML页面上分别将其呈现为Bag和Shirt上的不同<td>

1 个答案:

答案 0 :(得分:0)

您可以像遍历事务数组一样循环遍历itemNamecartId等值。

所以,而不是:

<td>{{itemName}}</td>

尝试类似的东西:

{{#itemName}}
  <td>{{itemName}}</td>
{{/itemName}}

请参阅标题为“非空列表”的mustache docs