简体   繁体   English

如何遍历车把中的 JSON 对象

[英]How to iterate through JSON object in handlebar

I'm trying to make a GET request and want to output the query that been sent.我正在尝试发出 GET 请求并希望输出已发送的查询。

app.get('/', function(req, res){
var myQuery = req.query;
console.log(myQuery);
res.render('home', myQuery);});  

After I submit a query and I console log the query, I get在我提交查询并控制台记录查询后,我得到

Express started on http://localhost:5200; press Ctrl-C to terminate.
{ parameter1: 'value1',
  parameter2: 'value2',
  parameter3: 'value3' } 

in the handlebar file, i do在车把文件中,我做

<ul>
{{#each this}}
<li> {{@key}}:{{@value}}</li>
{{/each}}
</ul 

When I render an HTML code, I get当我呈现 HTML 代码时,我得到

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<ul>
<li> settings:</li>
<li> parameter1:</li>
<li> parameter2:</li>
<li> parameter3:</li>
<li> _locals:</li>
<li> cache:</li>
</ul>
</body>
</html>

I'm expecting the body to look like this我期待身体看起来像这样

 <body>
    <ul>
    <li> parameter1:value1</li>
    <li> parameter2:value2</li>
    <li> parameter3:value3</li>
    </ul>
    </body>

I figured it out我想到了

I replaced the code in my handlebars file with我将车把文件中的代码替换为

{{#each myData}}
<li> {{@key}} : {{@this}}</li>
{{/each}}
</ul> 

and I rendered the query as我将查询呈现为

app.get('/', function(req, res){
    var myQuery = req.query;
    console.log(myQuery);
    res.render('home', {myData:myQuery});

}); 

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

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