gpt4 book ai didi

javascript - 如何将数据从 express 传递到我的 hbs View

转载 作者:行者123 更新时间:2023-11-30 11:55:17 25 4
gpt4 key购买 nike

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 View 。我的 hbs View 如下。我只是想遍历数据。

<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>

最佳答案

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

关于javascript - 如何将数据从 express 传递到我的 hbs View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com