如何处理车把中具有未知键的json对象的嵌套数组

时间:2019-08-02 12:03:56

标签: node.js json handlebars.js render

我有一个针对数据库的查询,该查询返回了多个JSON对象作为响应,因此使用-来创建有效的JSON

 var intObj = JSON.parse("[" + response + "]");
 console.log(JSON.stringify(intObj));
 resp.render('abc', {details : intObj});

console.log输出/ intObj-

    [{
    "0c057c2b-a4fa-4b8a-89b9-abebf2006704": [{
        "tah": {
            "id": "92b40e40-58d2-48ff-bfbc-9318b9637ffb",
            "ldt": 1564715961014
        }
    }, {
        "th": {
            "ldt": 1564715952873
        }
    }, {
        "aah": {
            "id": "eb90c2d1-95a1-4f4c-97c3-cbc55a95c57a",
            "ldt": 1564715961019
        }
    }, {
        "ah": {
            "ldt": 1564715952885
        }
    }, {
        "dec": {
            "re": "abc"
        }
    }]
    }, 
    {
    "7589668e-62b4-4339-ac06-7591ea1490d3": [{
    .....
        "dec": {
            "re": "test1"
        }
    }]
}]

我正在尝试在“ dec”对象下打印“ re”值,但什么也不显示,该怎么做?

    {{#each details}}
      {{#each ../dec}}     // tried  {{#each this}} and {{#each dec} and putting another {{#each this}} between details and dec no change in output
        <div class="row">{{re}}</div>
      {{/each}}
    {{/each}}

如果我将代码放在下面,它只会打印对象,所以我认为传递数据时没有验证问题-

{{#each details}}
    <div class="row">{{this}}</div>
{{/each}}

显示-

[object Object]
[object Object]

1 个答案:

答案 0 :(得分:-1)

尽管不是最佳方法,这也是我目前能够处理的方式-

const expresshandlebars = require('express-handlebars');
app.engine('handlebars', expresshandlebars({helpers: { json: function (context) { return JSON.stringify(context); } } }));

在把手中-

{{#each details}}
  {{#each this}}
    <div class="row">{{json this.[4].dec.re}}</div>
  {{/each}}
{{/each}}