gpt4 book ai didi

express - 返回一个值的 Handlebars

转载 作者:行者123 更新时间:2023-12-03 22:42:29 24 4
gpt4 key购买 nike

我想知道是否可以删除我的 Handlebars 模板值只返回一个值,尽管我路由我的 sql 调用的方式。基本上我的路线是查询我的一个表(表:图像)的所有记录,同时包括第二个表,其中有一个记录(表:描述)与所有被查询的记录相关联。结果,每当我在页面中尝试此对象时,我只能将一个记录表(表:描述)的属性用作循环,它显示值的次数与显示的图像一样多。 Handlebars 内有没有办法将其限制为只有一个值?

这是我目前显示此记录表属性的方式:

{{#image}}
<h3>{{this.description.body}}</h3>
{{/image}}

这是我的路线:
router.get('/:pattern/:color/result', function(req, res, image){

console.log(req.params.color);
console.log(req.params.pattern);

db.Images.findAll({
where: {
pattern: req.params.pattern,
color: req.params.color
},
include: [{ model: db.Description, attributes: ['body']}],
attributes: ['id', 'pattern', 'color', 'imageUrl', 'imageSource', 'description_id', 'description.body']
}).then(function(image){
//console.log(doc.descriptions_id);
res.render('pages/result.hbs', {
pattern : req.params.pattern,
color : req.params.color,
image : image
})
});
});

这是我的 View 文件:
<div class="container">
<div class="row pattern-choice">
<div class="col-md-12">
<h2><i>What to wear</i></h2>
{{#image}}
<h3>{{this.description.body}}</h3>
{{/image}}
</div>
</div>
<div class="row pattern-choice">
<div class="col-md-12">

<h2><i>Inspiration</i></h2>
</div>
</div>
<div class="row">

{{#each image}}
<div class="col-lg-3 col-md-4 col-xs-6 thumb inspiration-image">
<ul>
<li>
<div class="image-placeholder">

<a href="{{ this.imageUrl }}" data-toggle="lightbox"><img class="img-responsive img-rounded" src="{{ this.imageUrl }}"></a>
</div>
<p><i>({{this.imageSource}})</i></p>
</li>
</ul>
</div>
{{/each}}
</div>
<div class="row">
<h3 class="button-choice"><a href="/" class="button-link" id="link-restart">TRY ON SOME MORE</a></h3>
</div>
</div>

最佳答案

this question关于使用 Handlebars 访问数组元素。这是一个例子:

<div class="col-md-12">
<h2><i>What to wear</i></h2>
<h3>{{image.[0].description.body}}</h3>
</div>
此外,您可能想考虑使用 limit option使用您的包含查询。

关于express - 返回一个值的 Handlebars ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33345522/

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