【问题标题】:Returning One Value Handlebars返回一个值的车把
【发布时间】:2015-10-29 01:07:09
【问题描述】:

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

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

{{#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
            })
        });
});

这是我的视图文件:

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

【问题讨论】:

    标签: express sequelize.js handlebars.js


    【解决方案1】:

    请参阅this question,了解使用把手访问数组元素。这是一个例子:

    <div class="col-md-12">
        <h2><i>What to wear</i></h2>
        <h3>{{image.[0].description.body}}</h3>
    </div>
    

    此外,您可能希望考虑在包含查询中使用 limit option

    【讨论】:

    • 再次感谢您的帮助。我试过this.description.body.[0],但这给了我正文字符串中的第一个字符,表示找到的图像数量。然后我尝试使用 limit 选项,它将我的图像和描述属性都限制为第一条记录。有没有办法可以将描述属性限制为第一条记录?限制代码:包括:[{ model: db.Description, attributes: ['body'], }], limit: 1, attributes: ['id', 'pattern', 'color', 'imageUrl', 'imageSource', 'description_id', 'description.body']
    • 感谢您的示例,但没有传递任何值,但产生了八个&lt;h3&gt; 标签,与我表中的图像记录数量相匹配。
    • 嗯,对我来说效果很好。您是否删除了{{#image}} 外壳?
    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    相关资源
    最近更新 更多