简体   繁体   English

如何使用车把助手打印JSON对象

[英]How to printing JSON objects using handlebar helpers

I have an object that I wanted to print on my html template (handlebars). 我有一个对象,我想在我的html模板(手柄)上打印。 The value is a count query from mysql server. 该值是来自mysql服务器的计数查询。 Here is the function that has query in it: 这是包含查询的函数:

function getGrade7Applicants(req, res, next) {
connection.query('SELECT COUNT(*) FROM applications WHERE grade="Grade 7" ', function (err, rows, fields) {
    if (err) {
        return next(err);
    };
    req._applications7 = rows;
    return next();
});
}

Here is my GET request on router file: 这是我对路由器文件的GET请求:

router.get('/dashboard', getGrade7Applicants, function (req, res, next) {
res.render('admission/dashboard', {
    applications: {
            'grade7': req._applications7
    }

});
});

The handlebar herlper is as below: 车把herlper如下:

Handlebars.registerHelper('toJSON', function(object){
return new Handlebars.SafeString(JSON.stringify(object));
});

And finally my html template is : 最后我的html模板是:

<div class="alert alert-success" role="alert">
            <p style="font-size:75px; text-align:center;">{{toJSON applications.grade7}}</p>               
            <p style="text-align:center;"><strong>7th GRADE Applications<strong> </p>
        </div>

After all this, the output on my web page is [{"COUNT(*)":20}] 毕竟,我网页上的输出是[{“COUNT(*)”:20}]

I don't really know why it prints part of the query. 我真的不知道为什么它打印部分查询。 20 is the right value that I wanted to print. 20是我想要打印的正确值。

As mentioned above, that is the property name, here is some further explanation: 如上所述,这是属性名称,这里有一些进一步的解释:

If you have the following query (i gave the count a name of result to make the display more display-friendly: 如果你有以下查询(我给了计数一个结果的名称,使显示更友好:

SELECT COUNT(*) as result FROM applications WHERE grade="Grade 7"

You should get back a result object that looks something like this: 你应该得到一个看起来像这样的结果对象:

[{
   "result": "20"
}]

So when you stringify it, you get the entire object including the prop name and brackets as text. 因此,当您对其进行字符串化时,您将获得整个对象,包括道具名称和括号作为文本。

You can pass in the whole array and iterate through it (what you have) in the template, or just the first element then accessing the property by name in the template: 您可以传入整个数组并在模板中迭代它(您拥有的),或者只是第一个元素,然后在模板中按名称访问属性:

res.render('admission/dashboard', {
    applications: {
        'grade7': req._applications7[0]
    }
});

and in the template: 并在模板中:

{{#if applications}}
    {{#if applications.grade7}}
        {{applications.grade7.result}}
    {{/if}}
{{/if}}

You can also just pass the value directly in the data: 您也可以直接在数据中传递值:

res.render('admission/dashboard', {
    applications: {
        'grade7': req._applications7[0].result //prop name specified
    }
});

And in your template: 在您的模板中:

{{#if applications}}
    {{#if applications.grade7}}
        {{applications.grade7}}
    {{/if}}
{{/if}}

You don't need to use the helper unless there is a very specific reason. 除非有特殊原因,否则您无需使用帮助程序。

That's not part of the query.. that's the name of field - since you haven't specified a field name, it just uses "count(*)". 这不是查询的一部分..这是字段的名称 - 因为您没有指定字段名称,它只使用“count(*)”。

You need to change you query to include a name you can work with like this: 您需要更改查询以包含您可以使用的名称,如下所示:

 Select count(*) as app_count from applications...

Then you can print the value by just using applications.gradet.app_count to get the value. 然后,您可以通过使用applications.gradet.app_count来获取值来打印该值。

I'm not 100% sure what your trying to achieve by stringifying an object as you'll have to re-parse it to get just the value of your object property out. 我不是百分之百地确定你想通过字符串化对象来实现什么,因为你必须重新解析它以获得你的对象属性的值。

I'm not that familiar with handlebars, so I could be leading you astray here. 我不熟悉车把,所以我可能会把你引入歧途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM