如何将数据从express传递到我的hbs视图

时间:2016-07-04 21:34:31

标签: javascript node.js express handlebars.js

var Ticket = require('./models/ticket.js');

module.exports = function (app)
{

    app.get('/',function (req, res)
    {
        Ticket.find({},function (err, tickets)
        {
            if(err)
            {
                res.send(err)
            }
            else
            {

                console.log(tickets);
                res.render('index',tickets);
            }
        });

    });

    app.get('/upload',function (req, res)
    {
        res.render('upload');
    });

    app.post('/upload',function (req, res)
    {
        var ticket = new Ticket();

        ticket.type = req.body.type;
        ticket.price = req.body.price;
        ticket.time = req.body.time;
        ticket.date = req.body.date;

        ticket.save(function(err)
        {
            if (err)
            {
                res.send(err);
            }
            console.log('Created');
            res.redirect('/');
        });
    });
};

上面是我的route.js文件,其中我有获取和发布数据的路线。我的get函数中的console.log()正在记录我的数据,但是我将这些数据传递给我的hbs视图时遇到了麻烦。我的hbs视图在下面。我只是试图遍历数据。

<div class="container-fluid">
    <div class="row">
{{#each tickets}}
        <div class="col m4 s12 l4">
            <div class="card blue-grey darken-1 z-depth-4">
                <div class="card-content white-text">
                    <span class="card-title">Date:{{date}}</span>
                    <ul>
                        <li>Time: {{time}}</li>
                        <li>Type:{{type}}</li>
                        <li>Price:&#8358; {{price}}</li>
                    </ul>
                </div>
                <div class="card-action">
                   <div class="row valign-wrapper">
                       <div class="col m4 s4 l4">
                           <p class="valign">Quantity:</p>
                       </div>
                       <div class="col m8 s8 l8">
                           <select class="browser-default">
                               <option value="" disabled selected>0</option>
                               <option value="1">1</option>
                               <option value="2">2</option>
                               <option value="3">3</option>
                               <option value="3">4</option>
                               <option value="3">5</option>
                               <option value="3">6</option>
                           </select>
                       </div>
                   </div>
                    <div class="row">
                        <div class="col m12">
                            <button class="btn bnt-link"><i class="fa fa-cart-plus" aria-hidden="true"></i></button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

{{/each}}

    </div>

</div>

1 个答案:

答案 0 :(得分:2)

res.render('index',{tickets:tickets});